How to create UIIMage from Hex Data?

privateson used Ask the Experts™
I am using XMPP and XMPP will give me a photo data like the following:

So I assume the above data is the photo data and I assume that it's Hex data. (maybe I am wrong)

So I use the following to create the UIImage, but it doesn't work
anyone know how to do it?

NSString* command = @"a3f549fa9705e7ead2905de0b6a804227ecdd404";
	command = [command stringByReplacingOccurrencesOfString:@" " withString:@""];
	NSMutableData *commandToSend= [[NSMutableData alloc] init];
	unsigned char whole_byte;
	char byte_chars[3] = {'\0','\0','\0'};
	int i;
	for (i=0; i < [command length]/2; i++) {
		byte_chars[0] = [command characterAtIndex:i*2];
		byte_chars[1] = [command characterAtIndex:i*2+1];
		whole_byte = strtol(byte_chars, NULL, 16);
		[commandToSend appendBytes:&whole_byte length:1]; 
	UIImage *image = [UIImage imageWithData: commandToSend];

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


Right now I just assume that "a3f549fa9705e7ead2905de0b6a804227ecdd404" is the Hex data of the image. Maybe it's not.
Any Idea what it is?

I'm afraid that only UIImage is about the Apple programming here. I can be wrong, I simply don't know this format and this XMPP.
I can ask a zone advisor to change the zones for this question, if you wish.

BTW, the standard way to convert a string to NSData is:
NSString* str= @"bla-bla-bla";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
NSData* data = [str dataUsingEncoding: NSASCIIStringEncoding];

This data should be the correct graphics format, same as a file, and so you can convert it to the NSImage.

I think, I have said it in one of your previous questions.

I asked the ZA to help.

I'm not familiar with XMPP protocal but it's obviously not a photo itself, and could be encoded already. Searching on google and found the following answer for your reference
What you need to do is converting the Delphi to Cocoa.
hope it helps.

To convert this Delphi code (mention in the comment above) is not a very big problem.

1. You can find a lot about Base64:
Base64 encoding options on the Mac and iPhone
The article contains the explanation and the code.

Base64 Encoding in Cocoa


A very long code is here too:

Here is a category of NSData (if it works, it is the most attractive solution):

2. Then something like:
NSImage *image = [[NSImage alloc] initWithData:data];
should work.

It will be great if you will test it and post here.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial