如何在Objective C中将十六进制数转换为整数和字符串? [英] How can I convert hex number to integers and strings in Objective C?

查看:140
本文介绍了如何在Objective C中将十六进制数转换为整数和字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出这样的数组的示例:

Given the example of an array like this:

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

我想在Objective C中获得每个指定项目的整数和字符串表示。一个ruby示例工作完美:

I want to get an integer and string representation of each of the specified items in Objective C. I have mocked up a ruby example which works perfectly:

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中实现?

How can I achieve this in Objective C?

推荐答案

你会这样做:

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

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

这篇关于如何在Objective C中将十六进制数转换为整数和字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆