1

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 :)

Community
  • 1
  • 1
user2755038
  • 17
  • 1
  • 3

2 Answers2

3

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];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

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

Helge Becker
  • 3,219
  • 1
  • 20
  • 33
  • Not necessarily. A UTF8 character can require anything between one and six bytes (or whose hex string representation of that is between two and twelve bytes). For example, many European characters outside the 7-bit ASCII character set take 16-bits each in UTF8. So do many Arabic characters. Etc. You cannot infer the string encoding solely from the number of bytes per character. In this case, OP is using `NSUTF8StringEncoding`, so we know it's UTF8 and the number of bytes per character is perfectly consistent with that. – Rob Dec 29 '13 at 13:48