2

I'm trying to get the correct base64 string by encoding a hex string. It works when I use converter websited but my App does not.

NSData* sentData = [combinedHexMessage dataUsingEncoding : NSUTF8StringEncoding];
NSLog (@"%@",sentData);
NSData* sentDataBase64 = [sentData base64EncodedDataWithOptions:0];
NSLog(@"%@",[NSString stringWithUTF8String:[sentDataBase64 bytes]]);

This is my code. combinedHexMessage looks like this in NSLog:

ffd8ffe000104a46494600010101006000600000ffdb004300020101020101020 ...

sentData :

66666438 66666530 30303130 34613436 34393436 30303031 30313031 ...

sentDataBase64 :

ZmZkOGZmZTAwMDEwNGE0NjQ5NDYwMDAxMDEwMTAwNjAwMDYwMDAwMGZmZGIwMDQzM ...

But it should look like:

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFB ...

Because this is the string I get after I paste my hex string there:

http://tomeko.net/online_tools/hex_to_base64.php?lang=en

What am I doing wrong?

deKay
  • 23
  • 6
  • 1
    http://stackoverflow.com/questions/11340587/converting-hex-to-base64-in-objective-c look at this – Sport Nov 18 '14 at 12:42
  • Can you step back and tell us what problem you're trying to solve? If you had binary data, you might base64 encode it (increasing the size by 1/3rd), or you might get hex string (doubling the size). But there's no point in getting base64 representation of hex string or vice versa. What precisely do you mean by "get correct base64 string by encoding a hex string" and why are you doing that? – Rob Nov 18 '14 at 12:48
  • @Rob The complete system looks like this: My iPad gets an hex string. There is no way I can change that because of the hardware architecture in the background. This hex string is an image I want to display. The string is also sent in serveral small pieces to the iPad but sticking them back together is already working. Now I need to display the image in a ImageView. From what i read here the easiest way would be taking the hex string, encoding it to base64 and then using NSURL and UIImage. [like here](http://stackoverflow.com/questions/1366837/how-to-display-a-base64-image-within-a-uiimageview) – deKay Nov 18 '14 at 13:02
  • No, that question is answering a very different question of how to convert base-64 string into an image (and by the way, I think the accepted answer is inadvisable; you should use the base64 methods as suggested by Jonathan). This is a very different problem, of how to convert a hex string into a `UIImage`, and calls for a completely different answer. – Rob Nov 18 '14 at 13:32

1 Answers1

1

If you have a hex string that represents an image, you simply want to convert that hex string to a NSData

NSString *hexadecimalString = ...
NSData *data = [hexadecimalString dataFromHexadecimalString];
self.imageView.image = [UIImage imageWithData:data];

Where dataFromHexadecimalString might be defined in a NSString category like so:

@implementation NSString (Hexadecimal)

- (NSData *)dataFromHexadecimalString
{
    // in case the hexadecimal string is from `NSData` description method (or `stringWithFormat`), eliminate
    // any spaces, `<` or `>` characters

    NSString *hexadecimalString = [self stringByReplacingOccurrencesOfString:@"[ <>]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [self length])];

    NSMutableData * data = [NSMutableData dataWithCapacity:[hexadecimalString length] / 2];
    for (NSInteger i = 0; i < [hexadecimalString length]; i += 2) {
        NSString *hexChar = [hexadecimalString substringWithRange: NSMakeRange(i, 2)];
        int value;
        sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
        uint8_t byte = value;
        [data appendBytes:&byte length:1];
    }

    return data;
}

@end

No base-64 conversion is needed in this process.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I dont know ehat I'm making wrong but `range:NSMakeRange(0, [self length])];` Doesn't work the error is `No visible @interface for ViewController declares the selector length`. And at `@Implementation ViewController` I get `Method definition for dataFromHExadecimalString: not found` – deKay Nov 18 '14 at 14:08
  • That `dataFromHexadecimalString` is to be implemented in a separate `NSString` [class category](https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Category.html), not within your view controller class. If you want, I can modify this method to show you what it would look like if you implemented it inside the view controller, but the right approach (IMHO) is really to put it in a class category like this. – Rob Nov 18 '14 at 14:10
  • Ok, now I made a new class "Hecadecimal.h/.m" and importet them into my Viewcontroller.m but says `No visible @interface for NSString declares the selector daraFromHexadecimalString` – deKay Nov 18 '14 at 14:31
  • You have to include the declaration of `dataFromHexadecimalString` in `@interface` in the .h and then make sure to `#import` the .h in your view controller's .m file. (By the way, the file naming convention for naming a class category for `NSString` would generally be `NSString+Hexadecimal.h`/`.m`, but the filename doesn't technically matter, so name it whatever you want.) – Rob Nov 18 '14 at 14:53