在 Objective-C 中处理大数字?

发布于 2025-01-04 14:44:12 字数 379 浏览 5 评论 0原文

我需要将 1393443048683555715 等值转换为十六进制。但是,首先,例如,我无法使用 NSLog() 将其显示为十进制。

好的,它有效:

NSLog(@"%qu", 1393443048683555706);

但是转换为十六进制怎么样?我必须使用什么类型来存储这么大的值?

NSLog([NSString stringWithFormat: @"%x", 1393443048683555706]);
// result eb854b7a. It's incorrect result!

但我忘了说这个大数字表示为字符串@“1393443048683555706”(不是int)

I need to convert values like 1393443048683555715 to HEX. But, first of all, i cann't display it as decimal using NSLog(), for example.

Ok, it works:

NSLog(@"%qu", 1393443048683555706);

But what about converting to HEX. What type i have to use to store this big value?

NSLog([NSString stringWithFormat: @"%x", 1393443048683555706]);
// result eb854b7a. It's incorrect result!

but i forgot to say that this big number represented as string @"1393443048683555706" (not int)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画尸师 2025-01-11 14:44:12

您可以将 %qi%qu 格式说明符与 NSLog 一起使用来显示 64 位整数。您的常量似乎适合 64 位有符号数,但有以下限制:

[−9223372036854775808 to 9223372036854775807]

You can use %qi and %qu format specifiers with NSLog to display 64-bit integers. Your constant appears to fit in 64-bit signed number, with the limits of:

[−9223372036854775808 to 9223372036854775807]
流心雨 2025-01-11 14:44:12

“x”格式说明符用于 32 位数字;您需要使用“qx”或“qX”(取决于您是否希望字母值为大写)。这些是无符号 long long 值的格式化程序,请参阅:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1

接下来,您不应该像上面那样直接将字符串传递给 NSLog - 这可能会导致崩溃。

NSLog(string); // bad!!
NSLog(@"%@", string); // good

因此,如果您的值作为字符串出现,您需要这样做:

NSString *longNumber = @"1393443048683555706";
NSLog(@"%qx", [longNumber longLongValue]);

如果字符串值无法强制转换为数字,longLongValue 将返回 0。我将把它留给您来处理错误(和边界)检查 - 请参阅 NSString 了解详细信息。

如果要将十六进制值保存为字符串,请执行以下操作:

NSString *hexRepresentation = [NSString stringWithFormat:@"%qx", [longNumber longLongValue]];

同样,最好注意错误处理。

The "x" format specifier is for 32-bit numbers; you need to use either "qx" or "qX" (depending on whether you want the letter values to be uppercase or not). These are the formatters for unsigned long long values, see:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1

Next, you should not pass a string as you have done above directly to NSLog - this can cause a crash.

NSLog(string); // bad!!
NSLog(@"%@", string); // good

So if your value comes as a string, you'll want to do this:

NSString *longNumber = @"1393443048683555706";
NSLog(@"%qx", [longNumber longLongValue]);

If the string value can't be coerced to a number, longLongValue will return 0. I'll leave it to you do handle the error (and bounds) checking - see NSString for details.

If you want to save the hex value as a string, do this:

NSString *hexRepresentation = [NSString stringWithFormat:@"%qx", [longNumber longLongValue]];

Again, best to take care for error handling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文