gpt4 book ai didi

ios - 将十六进制转换为二进制(大值)

转载 作者:行者123 更新时间:2023-11-28 22:21:22 26 4
gpt4 key购买 nike

-(NSString *)toBinary:(NSUInteger)input
{
if (input == 1 || input == 0)
return [NSString stringWithFormat:@"%u", input];
return [NSString stringWithFormat:@"%@%u", [self toBinary:input / 2], input % 2];
}

NSString *hex = txtHexInput.text;
NSUInteger hexAsInt;
[[NSScanner scannerWithString:hex] scanHexInt:&hexAsInt];
NSString *binary = [NSString stringWithFormat:@"%@", [self toBinary:hexAsInt]];
txtBinaryInput.text = binary;

上面的代码工作得很好......直到你需要超过 32 位。将大于 32 位值的十六进制转换为二进制的任何指针?谢谢。

最佳答案

您可以使用 uint64_tunsigned long long 获得 64 位。

-(NSString *)toBinary:(unsigned long long)input
{
if (input == 1 || input == 0)
return [NSString stringWithFormat:@"%llu", input];
return [NSString stringWithFormat:@"%@%llu", [self toBinary:input / 2], input % 2];
}

NSString *hex = txtHexInput.text;
unsigned long long hexAsULL;
[[NSScanner scannerWithString:hex] scanHexLongLong:&hexAsULL];
NSString *binary = [NSString stringWithFormat:@"%@", [self toBinary:hexAsULL]];
txtBinaryInput.text = binary;

这将为您提供从 0 到 18,446,744,073,709,551,615(十进制)的数字

关于ios - 将十六进制转换为二进制(大值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20294806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com