//傳入一個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,有時候不能,不知道為什么,請大家幫我看看,給點建議,如何才能轉換成功。
uj5u.com熱心網友回復:
nsscaner,可以把字串轉16進制intuj5u.com熱心網友回復:
Nsscaneruj5u.com熱心網友回復:
有些轉換容易歧義的uj5u.com熱心網友回復:
-(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;
}
不過是二位二位的轉的
uj5u.com熱心網友回復:
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;
}
uj5u.com熱心網友回復:
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/143129.html
標籤:iOS
上一篇:急需大佬們幫忙 需表白墻原始碼
