Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zippyfly

macrumors regular
Original poster
Mar 22, 2008
141
0
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.
 
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.

Use stringWithFormat.
 
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.
 
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.
 
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!
 
Oh wow, I totally misunderstood. The Hex thing didn't have much to do with it; ignore my %x answer.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.