如何将十六进制NSData转换为十进制数组?

时间:2013-10-04 10:01:41

标签: iphone ios

我有一个十六进制值的数组,如下所示:

const UInt8 request_MifareID[] = {
        0x00, 0xCA, 0x01, 0x0A, 0x00
    };

然后我将它转移到NSData进行通信:

NSData *data = [[NSData alloc] initWithBytesNoCopy:(UInt8 *)request_MifareID length:5];

将“数据”发送到服务器并获取服务器响应:

Printing description of resp:
<ee97562d 02280400 00000000 21074400 9000>

NSUInteger len = [resp length];

Printing description of len:
(NSUInteger) len = 18

“resp”也是十六进制格式NSData

现在,我必须将“resp”转移到Decimal数组

我该怎么办?

----------------------------------------------- -------修改

好的,首先

我必须传输NSData哪个服务器响应(它不是字符串,只是数字):

ee97562d 02280400 00000000 21074400 9000

进入什么样的对象?

字节?

当我使用

NSString * str = [[NSString alloc] initWithData: resp
                                           encoding:NSUTF8StringEncoding];

它返回nil

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

**NSData *data = [NSData dataWithBytes:"FF3C" length:4];
NSString *string = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
string = [@"0x" stringByAppendingString:string];

unsigned short value;
sscanf([string UTF8String], "%hx", &value);

NSLog(@"%d", value)**