How can I convert a string in hex format to retrieve from it an NSData and after that an UIImage inside of my iPhone app?
I've got a string (str) that contains a value like X'FFD8FFE000104A46....01010000010001000'.
That string is created from a xml file by the method:
- (void)parser:(NSXMLParser *)parse开发者_运维百科r foundCharacters:(NSString *)string {
str = [str stringByAppendingString:string];
}
How can I get the image back from that string?
Thanks in advance,
Andrea
For converting NSData to a NSString, try "initWithData:encoding:"
For converting NSData to an UIImage, try "imageWithData:"
You may find GTMStringEncoding helpful, from Google Toolbox For Mac.
my sample code , updated!
-(NSData*)toString:(NSString*)uniStr
{
int len=[uniStr length]/4;
unichar *oux=alloca(len*sizeof(unichar));
unichar *p = oux;
for (int i=0; i<[uniStr length]/4; i++) {
NSInteger first= i*4;
NSUInteger a =toInde([uniStr characterAtIndex:first])<<4;//3 3c
NSUInteger b =toInde([uniStr characterAtIndex:(first+1)]);//2
NSUInteger c =toInde([uniStr characterAtIndex:(first+2)])<<12 ;//0 68
NSUInteger d =toInde([uniStr characterAtIndex:(first+3)])<<8;//0
unichar x = a+b+c+d;//十进制 3c68->683c
memcpy(p, &x, 2);
p++;
}
return [NSData dataWithBytes:oux length:len*2];
}
int toInde(int x)
{
char b=0;
sprintf(&b, "%c",a);
return b;
}
精彩评论