//传入一个16进制字符串:@"328A6102033B516386668561DF5F0B7E"
-(void)getByteWithString:(NSString *)string
{
    NSMutableArray *strMutArr = [[NSMutableArray alloc]init];
    for (int i = 0; i < string.length; i += 2)
    {
        NSString *str = [NSString stringWithFormat:@"0x%@",[string substringWithRange:NSMakeRange(i, 2)]];
        [strMutArr bd_addObject:str];
    }
    //strMutArr打印结果:Printing description of strMutArr:<__NSArrayM 0x145db8730>(0x32,0x8A,0x61,0x02,0x03, 0x3B,0x51,0x63,0x86,0x66,0x85,0x61,0xDF,0x5F,0x0B,0x7E)
    Byte bytes[16];
    for (int j = 0; j < strMutArr.count; j++)
    {
        const char *c = [strMutArr[j] UTF8String];
        Byte byte = (Byte)strtol(c, NULL, 16);
        bytes[j] = byte;
    }
    //bytes结果:(Byte [16]) bytes = {
                                //    [0] = '2'
                                //    [1] = '\x8a'
                                //    [2] = 'a'
                                //    [3] = '\x02'
                                //    [4] = '\x03'
                                //    [5] = ';'
                                //    [6] = 'Q'
                                //    [7] = 'c'
                                //    [8] = '\x86'
                                //    [9] = 'f'
                                //    [10] = '\x85'
                                //    [11] = 'a'
                                //    [12] = '\xdf'
                                //    [13] = '_'
                                //    [14] = '\v'
                                //    [15] = '~'
                                    //}
}
现在想要的结果是和strMuatle结果一一对应如下:
bytes结果:(Byte [16]) bytes = {
    [0] = '\x32'
    [1] = '\x8a'
    [2] = '\x61'
    [3] = '\x02'
    [4] = '\x03'
    [5] = '\x3b'
    [6] = '\x51'
    [7] = '\x63'
    [8] = '\x86'
    [9] = '\x66'
    [10] = '\x85'
    [11] = '\x61'
    [12] = '\xdf'
    [13] = '\x5f'
    [14] = '\x0b'
    [15] = '\x7e'
}
但一直不能成功....Byte byte = (Byte)strtol(c, NULL, 16); 这个方法对于一些十六进制数有时候可以转化成对应的byte,有时候不能,不知道为什么,请大家帮我看看,给点建议,如何才能转换成功。

解决方案 »

  1.   

    nsscaner,可以把字符串转16进制int
      

  2.   

    Nsscaner
      

  3.   

    -(NSData*)hexToBytes:(NSString*)str{
        NSMutableData* data = [NSMutableData data];
        int idx;
        for (idx = 0; idx+2 <= str.length; idx+=2) {
            NSRange range = NSMakeRange(idx, 2);
            NSString* hexStr = [str substringWithRange:range];
            NSScanner* scanner = [NSScanner scannerWithString:hexStr];
            unsigned int intValue;
            [scanner scanHexInt:&intValue];
            [data appendBytes:&intValue length:1];
        }
        return data;
    }
    不过是二位二位的转的
      

  4.   

    NSLog(@"data %@", [self dataConvertToHexString:@"328A6102033B516386668561DF5F0B7E"]);打印结果:data <328a6102 033b5163 86668561 df5f0b7e>
    - (NSData *)dataConvertToHexString:(NSString *)hexString
    {
        const char *chars = [hexString UTF8String];
        int i = 0;
        NSUInteger len = hexString.length;
        NSMutableData *data = [NSMutableData dataWithCapacity:len / 2];
        char byteChars[3] = {'\0','\0','\0'};
        unsigned long wholeByte;
        while (i < len) {
            
            byteChars[0] = chars[i++];
            byteChars[1] = chars[i++];
            
            wholeByte = strtoul(byteChars, NULL, 16);
            [data appendBytes:&wholeByte length:1];
        }
        
        return data;
    }
      

  5.   

    NSLog(@"%@",[self hexStringToByte:@"328A6102033B516386668561DF5F0B7E"]);//从字符串中取字节数组 转成NSdaTa
    -(NSData *)hexStringToByte:(NSString*)string {
        
        NSString *hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@"-" withString:@""];
        if ([hexString length]%2!=0) {
            return nil;
        }
        NSData * data = [hexString dataUsingEncoding:NSUTF8StringEncoding];
        Byte *testByte = (Byte *)[data bytes];
        Byte testByteArr[16] = {0};
        for (int i=0; i<16; i++) {
            int post = i*2;
            testByteArr[i] = (Byte)(([self toByte:testByte[post]]<<4)|([self toByte:testByte[post+1]]));
             NSLog(@"====1122==%02x",testByteArr[i]);
    //         NSLog(@"====1122==%d",testByteArr[i]);
        }
        return [NSData dataWithBytes:testByteArr length:16];
    }2019-06-11 10:05:57.401682+0800 UDPSocket[2619:831828] ====1122==32
    2019-06-11 10:05:57.401710+0800 UDPSocket[2619:831828] ====1122==8a
    2019-06-11 10:05:57.401718+0800 UDPSocket[2619:831828] ====1122==61
    2019-06-11 10:05:57.401725+0800 UDPSocket[2619:831828] ====1122==02
    2019-06-11 10:05:57.401732+0800 UDPSocket[2619:831828] ====1122==03
    2019-06-11 10:05:57.401739+0800 UDPSocket[2619:831828] ====1122==3b
    2019-06-11 10:05:57.401746+0800 UDPSocket[2619:831828] ====1122==51
    2019-06-11 10:05:57.401753+0800 UDPSocket[2619:831828] ====1122==63
    2019-06-11 10:05:57.401760+0800 UDPSocket[2619:831828] ====1122==86
    2019-06-11 10:05:57.401766+0800 UDPSocket[2619:831828] ====1122==66
    2019-06-11 10:05:57.401773+0800 UDPSocket[2619:831828] ====1122==85
    2019-06-11 10:05:57.401779+0800 UDPSocket[2619:831828] ====1122==61
    2019-06-11 10:05:57.401785+0800 UDPSocket[2619:831828] ====1122==df
    2019-06-11 10:05:57.401792+0800 UDPSocket[2619:831828] ====1122==5f
    2019-06-11 10:05:57.401798+0800 UDPSocket[2619:831828] ====1122==0b
    2019-06-11 10:05:57.401805+0800 UDPSocket[2619:831828] ====1122==7e
    2019-06-11 10:05:57.401849+0800 UDPSocket[2619:831828] <328a6102 033b5163 86668561 df5f0b7e>