我有这个字符串
NSString * str = @ "01 00 00 41 8B 00 01 00 00 40 BC 00 01 00 00 43 0D 00";
我尝试将此转换为NSacii
- (NSString *)hexToString:(NSString *)string {
NSMutableString * newString = [[NSMutableString alloc] init];
NSScanner *scanner = [[NSScanner alloc] initWithString:string];
unsigned value;
while([scanner scanHexInt:&value]) {
[newString appendFormat:@"%c",(char)(value & 0xFF)];
}
string = [newString copy];
[newString release];
return [string autorelease];
}
它工作得很完美,但问题是当我想将字符串转换为十六进制时我无法找到 00表示结果为01 41 8B 01 40 BC 01 43 0D
答案 0 :(得分:0)
- (NSString *)hexToString:(NSString *)string {
NSMutableString * newString = [[NSMutableString alloc] init];
NSScanner *scanner = [[NSScanner alloc] initWithString:string];
unsigned value;
while([scanner scanHexInt:&value]) {
if (value==0 )
{
[newString appendString:@"\0"];
}
else
{
[newString appendFormat:@"%c",(char)(value & 0xFF)];
}
}
string = [newString copy];
[newString release];
return [string autorelease];
}