Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Hexadecimal string to NSData doesn't work

igorladessaigorladessa Posts: 27New Users @
I'm getting from the webservice this hexadecimal

http://www.moodpin.it/cms/listar_avatar?id=9

and trying to convert to NSData and set the UIImageView but it doesn't work

Here is my code:

NSData *dataImage = [appDelegate hexStringToData:avatar.avatar];

UIImage *imagem = [[UIImage alloc]initWithData:dataImage];
UIImageView *imageView = [[UIImageView alloc]init];

[imageView setImage:imagem];

- (NSData *) hexStringToData:(NSString *) hexString
{
unsigned char whole_byte;
NSMutableData *returnData= [[NSMutableData alloc] init];
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [hexString length]/2; i++) {
byte_chars[0] = [hexString characterAtIndex:i*2];
byte_chars[1] = [hexString characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[returnData appendBytes:&whole_byte length:1];
}
return (NSData *) [returnData autorelease];
}

Replies

  • igorladessaigorladessa Posts: 27New Users @
    any idea what's wrong?
  • RhadeRhade Posts: 661Registered Users @ @ @
    Do not quote questionable posts.
    Do not post moderator requests in public. Send a PM.
    vvvvv ---- Use the flag button to report spam.
  • Duncan CDuncan C Posts: 9,116Tutorial Authors, Registered Users @ @ @ @ @ @ @

    I'm getting from the webservice this hexadecimal

    http://www.moodpin.it/cms/listar_avatar?id=9

    and trying to convert to NSData and set the UIImageView but it doesn't work

    Here is my code:

    NSData *dataImage = [appDelegate hexStringToData:avatar.avatar];

    UIImage *imagem = [[UIImage alloc]initWithData:dataImage];
    UIImageView *imageView = [[UIImageView alloc]init];

    [imageView setImage:imagem];

    - (NSData *) hexStringToData:(NSString *) hexString
    {
    unsigned char whole_byte;
    NSMutableData *returnData= [[NSMutableData alloc] init];
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    for (i=0; i < [hexString length]/2; i++) {
    byte_chars[0] = [hexString characterAtIndex:i*2];
    byte_chars[1] = [hexString characterAtIndex:i*2+1];
    whole_byte = strtol(byte_chars, NULL, 16);
    [returnData appendBytes:&whole_byte length:1];
    }
    return (NSData *) [returnData autorelease];
    }


    Several things.

    First, please see the link How To Make Your Code Look Nice and edit your post to use proper code tags. Code without proper code tags is MUCH harder to read.


    Second, you say "...it doesn't work". That is perhaps the least useful thing you can say about a block of code. What do you mean it doesn't work? You posted code that is supposed to take a block of string data and convert it to an NSMutableData object that contains the bytes described in the hex string.

    What makes you think it's not working? What have you done to troubleshoot this code? At a glance it looks reasonable. In order to figure out what's wrong with it I'd have to create a test application, copy this code into a method in the test app, and run it through the debugger to figure out what's wrong with it.

    However, that's your job, not your readers'.

    If you do your homework and provide a more detailed description of what you're trying, including the string you're passing in and the data you get back, and what you think is going wrong, we can help you.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • igorladessaigorladessa Posts: 27New Users @
    It does'nt work = when I convert the hexadecimal string to NSData and set to an UIImage, this UIImage is nil.

    I did post the link to the webservice from I got the hexa string...
    The string that I'm trying to convert is too long to post here...
  • Duncan CDuncan C Posts: 9,116Tutorial Authors, Registered Users @ @ @ @ @ @ @

    It does'nt work = when I convert the hexadecimal string to NSData and set to an UIImage, this UIImage is nil.

    I did post the link to the webservice from I got the hexa string...
    The string that I'm trying to convert is too long to post here...

    When a multi-step block of code fails to give the correct result, you have to break it down and test the individual steps until you find the problem.

    UIImage will return nil if the data is not formatted properly as image data. That suggests that your data is getting corrupted somehow.

    First take your hex string and convert it to NSData. Then look at the bytes of NSData in memory to see if they appear to contain the correct data. (The Xcode debugger includes a view memory feature that lets you examine the contents of memory. Check the data at the beginning and at the end. Also check the length to make sure the data is the correct length.

    Look at your string of hex characters and see if it contains any spaces, carriage returns, prefixes to indicate that it's hex (e.g. "$" or "0X", etc.

    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
Sign In or Register to comment.