我有一个六进制十进制数
535443326663315634524877795678586b536854535530342f44526a795744716133353942704359697a6b736e446953677171555473
我想将此数字转换为ASCII格式,如下所示
STC2fc1V4RHwyVxXkShTSU04/DRjyWDqa359BpCYizksnDiSgqqUTsYUOcHKHNMJOdqR1/TQywpD9a9xhri
我在这里看过解决方案,但没有一个对我有用 NSString containing hex convert to ascii equivalent
我在这里查了但是他们给出了不同的结果。任何帮助
答案 0 :(得分:1)
这完美无缺
- (NSString *)stringFromHexString:(NSString *)hexString {
// The hex codes should all be two characters.
if (([hexString length] % 2) != 0)
return nil;
NSMutableString *string = [NSMutableString string];
for (NSInteger i = 0; i < [hexString length]; i += 2) {
NSString *hex = [hexString substringWithRange:NSMakeRange(i, 2)];
NSInteger decimalValue = 0;
sscanf([hex UTF8String], "%x", &decimalValue);
[string appendFormat:@"%c", decimalValue];
NSLog(@"string--%@",string);
}
_hexString1=string;
NSLog(@"string ---%@",_hexString1);
return string;
}
答案 1 :(得分:0)
如果您以NSData *
开头,则可以通过以下方式获取ASCII字符串:
NSData *someData = [NSData dataWithHexString:@"ABC123"];
NSString *asciiString = [[NSString alloc] initWithData: someData encoding:NSASCIIStringEncoding];