How to express ASCII codes from variables in NSString?

Discussion in 'Mac Programming' started by zippyfly, Sep 23, 2010.

  1. macrumors regular

    Joined:
    Mar 22, 2008
    #1
    Hi. I am trying to iterate through the ASCII codes (just as an experiment) and not sure how I can express the code in the string literal.

    For example, within the for loop, with i going from 1 to 255, I want to print:

    Code:
    NSLog(@"Character is \x..."
    
    and that is where I am stuck.

    For instance, if i = 65 (which is ASCII character "A") then the hex value is 41.

    Literally, the string would be

    Code:
    @"\x41"
    but since I am using the variable i to iterate, how do I 1) convert the decimal to hex and 2) express that inside the NSLog string literal?

    I Googled around and found two class methods for the dec/hex conversion but the above is still unclear. I suspect I need to cast the string to a value and then also somehow "escape" that value into the string literal.

    Code:
    NSString * decimalString = [NSString stringWithFormat:@"%d", number];
    NSString * hexString = [NSString stringWithFormat:@"%x", number];
    
    Thanks.
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Use stringWithFormat.
     
  3. macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #3
    Use the %c format specifier.

    Code:
    NSLog(@"%c", 65);
    prints A.
     
  4. macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #4
    You can also use stringbyencoding and specify an encoding type as well.
     
  5. macrumors 6502

    Joined:
    Apr 24, 2008
    #5
    Maybe I'm missing something, but why don't you simply use the %x format specifier in your NSLog string? NSLog is a vararg function.
     
  6. macrumors regular

    Joined:
    Oct 13, 2008
    Location:
    Achewood, CA
    #6
    It sounds like the OP is saying, in a very roundabout way, "I have this number, how do I print the ASCII character it represents?"

    If that's true, see my post above. The format specifier that outputs an ASCII character is %c.

    Trying to create an NSString with an \x... escape code in it will not work, because character escape sequences are processed at compile time, not runtime.
     
  7. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #7
    ASCII ends with 127. What encoidng are you using for 128~255?
     
  8. thread starter macrumors regular

    Joined:
    Mar 22, 2008
    #8
    Many thanks!

    Yes, apologies for the verbose description; I was just transcribing my thought process which obviously went off on a tangent since I was not aware of %c (actually I was not aware to even look there due to being caught up about the \x sequence, and on that point thanks for letting me know that's only for compile time).

    Regarding the ASCII range, yes I had known I'd run into non-printable characters but thought I'd figure that out later.

    Thanks everyone!
     
  9. macrumors 6502

    Joined:
    Apr 24, 2008
    #9
    Oh wow, I totally misunderstood. The Hex thing didn't have much to do with it; ignore my %x answer.
     

Share This Page