如何在目标C中将十六进制数转换为整数和字符串?

时间:2009-06-01 13:20:01

标签: 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中实现这一目标?

2 个答案:

答案 0 :(得分:10)

要获得十进制和十六进制等值,您可以这样做:

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

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

答案 1 :(得分:0)

一旦你有一个NSString,你可以使用该类中的方法,如intValue或longValue或floatValue

相关问题