10

I have a NSString with hex string like "68656C6C6F" which means "hello".

Now I want to convert the hex string into another NSString object which shows "hello". How to do that ?

RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
user403015
  • 7,209
  • 19
  • 66
  • 100

5 Answers5

23

I am sure there are far better, cleverer ways to do this, but this solution does actually work.

NSString * str = @"68656C6C6F";
NSMutableString * newString = [[[NSMutableString alloc] init] autorelease];
int i = 0;
while (i < [str length])
{
    NSString * hexChar = [str substringWithRange: NSMakeRange(i, 2)];
    int value = 0;
    sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
    [newString appendFormat:@"%c", (char)value];
    i+=2;
}
RedBlueThing
  • 42,006
  • 17
  • 96
  • 122
5

This should do it:

- (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];
    }

    return string;
}
Morten Fast
  • 6,322
  • 27
  • 36
  • [hex UTF8String] should be [hex cStringUsingEncoding:NSASCIIStringEncoding] if it's hex it's ASCII – valexa Feb 09 '14 at 05:50
0
extension String {
    func hexToString()-> String {
        var newString = ""
        var i = 0
        while i < self.count {
            let hexChar = (self as NSString).substring(with: NSRange(location: i, length: 2))
            if let byte = Int8(hexChar, radix: 16) {
                if (byte != 0) {
                    newString += String(format: "%c", byte)
                }
            }
            i += 2
        }
        return newString
    }
}
김학철
  • 1
  • 1
0
+(NSString*)intToHexString:(NSInteger)value
{
return [[NSString alloc] initWithFormat:@"%lX", value];
}
Aditya
  • 4,414
  • 5
  • 29
  • 40
  • Should I input "68656C6C6F" into the argument value ? Also I think your solution is not correct. – user403015 Jun 21 '11 at 06:42
  • 6
    Not only is it not correct but it also breaks the ownership rule. Happy debugging/leak fixing. – JustSid Jun 21 '11 at 06:46
  • I think this is the best answer, i added more sample code in an answer furhter down – dancl Jul 23 '11 at 14:44
  • This answer should be removed. The question is about converting a string containing hexadecimal data into a string containing the text that the hexadecimal data represents. – dreamlax Aug 23 '12 at 03:48
-1

I think the people advising initWithFormat is the best answer as it's objective-C rather than a mix of ObjC, C.. (although the sample code is a bit terse).. I did the following

unsigned int resInit = 0x1013;
if (0 != resInit)
{
    NSString *s = [[NSString alloc] initWithFormat:@"Error code 0x%lX", resInit];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Initialised failed"
        message:s
        delegate:nil
        cancelButtonTitle:@"OK"
        otherButtonTitles:nil];
    [alert show];
    [alert release];
    [s release];
}
dancl
  • 689
  • 5
  • 13