user2755038
user2755038

Reputation: 17

convert hex to NSString

i converted to hex by:

NSString * hexStr = [NSString stringWithFormat:@"%@",
                                  [NSData dataWithBytes:[string cStringUsingEncoding:NSUTF8StringEncoding] 
                                                 length:strlen([string cStringUsingEncoding:NSUTF8StringEncoding])]];

i converted from foreign language so each character is now 4 bytes in hex. i have tried to convert it back to nsstring but i didnt get the original string (by How to convert HEX to NSString in Objective-C?).

help :)

Upvotes: 1

Views: 3246

Answers (2)

Rob
Rob

Reputation: 437372

A couple of observations:

  1. Your technique for converting the string to a NSData by getting cStringUsingEncoding and then using that to dataWithBytes is a little indirect. It's much easier to do:

    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    
  2. If you want to convert a NSData back to a NSString, you can do:

    NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
  3. You're successfully using stringWithFormat to create a hex string representation of your NSData. Now the question is how to convert that hex string representation back to a NSData. Lots of techniques, but here's one routine that does that:

    - (NSData *)dataFromHexString:(NSString *)originalHexString
    {
        NSString *hexString = [originalHexString stringByReplacingOccurrencesOfString:@"[ <>]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [originalHexString length])]; // strip out spaces (between every four bytes), "<" (at the start) and ">" (at the end)
        NSMutableData *data = [NSMutableData dataWithCapacity:[hexString length] / 2];
        for (NSInteger i = 0; i < [hexString length]; i += 2)
        {
            NSString *hexChar = [hexString substringWithRange: NSMakeRange(i, 2)];
            int value;
            sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
            uint8_t byte = value;
            [data appendBytes:&byte length:1];
        }
    
        return data;
    }
    

So, putting this all together, to

NSString *string          = @"こんにちは、世界。";
NSData   *data            = [string dataUsingEncoding:NSUTF8StringEncoding];
NSString *hexString       = [NSString stringWithFormat:@"%@", data];
NSData   *resultingData   = [self dataFromHexString:hexString];
NSString *resultingString = [[NSString alloc] initWithData:resultingData encoding:NSUTF8StringEncoding];

Upvotes: 3

Helge Becker
Helge Becker

Reputation: 3243

Each char is 4 bytes in hex? That means it is actually four nibbles, so it is not UTF 8 but UTF 16.

Upvotes: 0

Related Questions