objective-c - 如何在 Objective C 中将十六进制数转换为整数和字符串?

标签 objective-c cocoa hex

给定这样一个数组的例子:

idx = [ 0xe, 0x3,  0x6, 0x8, 0x2 ]

我想获得 Objective C 中每个指定项目的整数和字符串表示。我模拟了一个完美运行的 ruby​​ 示例:

0xe gives 14 when i run 0xe.to_i and "e" when i run to_i(base=16)
0x3 gives 3 when i run 0x3.to_i and gives 3 when i run to_i(base=16) 

如何在 Objective C 中实现这一目标?

最佳答案

要获得十进制和十六进制等价物,您可以:

int number = 0xe; // or 0x3, 0x6, 0x8, 0x2

NSString * decimalString = [NSString stringWithFormat:@"%d", number];
NSString * hexString = [NSString stringWithFormat:@"%x", number];

关于objective-c - 如何在 Objective C 中将十六进制数转换为整数和字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/934790/

相关文章:

ios - 应用程序不在后台运行

cocoa - MonoMac 中的 NSReadPixel 在哪里?

objective-c - SMJobRemove 成功,但未删除 plist 和 helper 工具

objective-c - 在 XCode 调试器中显示 NSDecimal 值

multithreading - 在Apple的Cocoa API中,为什么从主线程调用NSApplicationMain很重要?

将十六进制转换为纬度和经度所需的 PHP 函数

c - 字符串到十六进制代码的奇怪问题

python - 将 numpy 数组转换为十六进制字节数组

ios - iPhone写入文件失败

cocoa - 为什么 AVPlayerItem 不支持 AVAudioTimePitchAlgorithmVarispeed 设置?