0

我有这个字符串

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

4

1 回答 1

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];
}
于 2012-06-14T15:41:03.823 回答