将GUID转换为NSString - 目标C.

时间:2014-06-09 09:37:54

标签: ios objective-c guid uuid endianness

我已经定义了GUID结构,如下所示

typedef struct _GUID {
    unsigned long  Data1;
    unsigned short Data2;
    unsigned short Data3;
    unsigned char  Data4[ 8 ];

} GUID;

我正在尝试将其转换为NSString。但是,我得到前三个(long, short,short)的反转值。这可能是由于 Big-endian

示例:

 GUID testGUID = { 0x865f82d0, 0x3303, 0x403d, { 0xbf, 0xcd, 0xeb, 0xb2, 0xde, 0xd9, 0xa0, 0x69 } };
 NSUUID *uuid = [[NSUUID alloc] initWithUUIDBytes:(const unsigned char*)&testGUID];
 NSString *uuidString = [uuid UUIDString];
 NSLog(@"%@", uuidString);

输出D0825F86-0333-3D40-BFCD-EBB2DED9A069

预计865F82D0-3303-403D-BFCD-EBB2DED9A069

让我知道有没有办法纠正这个问题。对此有任何帮助非常感谢。

1 个答案:

答案 0 :(得分:1)

@borrrden评论解决了这个问题。

这是解决方案。

GUID testGUID = { 0x865f82d0, 0x3303, 0x403d, { 0xbf, 0xcd, 0xeb, 0xb2, 0xde, 0xd9, 0xa0, 0x69 } };

testGUID.Data1 = NTOHL(testGUID.Data1);
testGUID.Data2 = NTOHS(testGUID.Data2);
testGUID.Data3 = NTOHS(testGUID.Data3);

NSUUID *uuid = [[NSUUID alloc] initWithUUIDBytes:(const unsigned char*)&testGUID];
NSString *uuidString = [uuid UUIDString];
NSLog(@"%@", uuidString)