Object-C

(iOS, Object-C)NSData转String,各进制

2017-10-19  本文已影响55人  raymondCaptain

Object-C

NSData 转 String:

- (NSString *)stringFromData:(NSData *)data {
    return [NSString stringWithFormat:@"%@", data];
}

十六进制转整形:

// 根据输入字符串长度需要考虑返回值的字节数
- (long)integerFromHEXStr:(NSString *)HEXStr {
    return strtol([HEXStr UTF8String], nil, 16);
}

n进制转整形:

// 根据输入字符串长度需要考虑返回值的字节数
- (long)integerFromStr:(NSString *)str {
    return strtol([str UTF8String], nil, n);
}

整形转 NSData:

- (NSData *)dataFromInteger:(int)integer {
    return [NSData dataWithBytes:&integer length:8];
}

十六进制字符串转 NSData:

- (NSData *)dataFromString:(NSString *)str {
    long longNumber = strtol([str UTF8String], nil, 16);
    NSData *data = [NSData dataWithBytes:&longNumber length:3];
    return data;
}
上一篇 下一篇

猜你喜欢

热点阅读