NSDATA to NSSTRING

Discussion in 'iOS Programming' started by kraitz, Nov 22, 2011.

  1. kraitz macrumors newbie

    Joined:
    Nov 22, 2011
    #1
    Hi,

    I'm trying to convert a NSDATA string consisting of hex to NSString.

    NSDATA : 4c000230 30303037 34323030

    i convert with this statement:
    Code:
    NSString *msg =  [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding] autorelease]; 
    but when i print msg, it only converts the first character. which is L. i realized that it always terminates at NULL (00) and the rest will be blank. How do i convert the while thing?
     
  2. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    So you are saying that your NSData object contains some data, and you want the hex representation of the bytes in that data as an NSString?
     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    No, I believe he said the first character was "L", so he wants the actual text from the data, not the hex. (I can help clarify the question, I don't know the answer. I would suspect googling would turn up some answers from Stack Overflow.)
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Post the code that prints the NSString.

    You're converting from an NSData object, so why not use initWithData:encoding:?

    After conversion to NSString, exactly what is the string's length?
     

Share This Page