How to create UIIMage from Hex Data?

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

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

Comment
Watch Question

Do more with

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

Author

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

Commented:
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.

Commented:
BTW, the standard way to convert a string to NSData is:
NSString* str= @"bla-bla-bla";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
or
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.

Commented:
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
http://stackoverflow.com/questions/1366423/how-do-you-display-an-xmpp-jabber-vcard-photo-in-delphi
What you need to do is converting the Delphi to Cocoa.
hope it helps.

Wu
Commented:
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
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
The article contains the explanation and the code.

Base64 Encoding in Cocoa
http://www.dribin.org/dave/blog/archives/2006/03/12/base64_cocoa/

BaseSixtyFour:
http://www.cocoadev.com/index.pl?BaseSixtyFour

A very long code is here too:
http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMBase64.m

Here is a category of NSData (if it works, it is the most attractive solution):
http://colloquy.info/project/browser/trunk/Additions/NSDataAdditions.m

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