PDA

View Full Version : How to express ASCII codes from variables in NSString?




zippyfly
Sep 24, 2010, 12:46 AM
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:

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

@"\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.

NSString * decimalString = [NSString stringWithFormat:@"%d", number];
NSString * hexString = [NSString stringWithFormat:@"%x", number];

Thanks.



gnasher729
Sep 24, 2010, 01:37 AM
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:

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

@"\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.

NSString * decimalString = [NSString stringWithFormat:@"%d", number];
NSString * hexString = [NSString stringWithFormat:@"%x", number];

Thanks.

Use stringWithFormat.

autorelease
Sep 24, 2010, 02:48 AM
Use the %c format specifier.

NSLog(@"%c", 65); prints A.

KoolStar
Sep 24, 2010, 06:55 AM
You can also use stringbyencoding and specify an encoding type as well.

Sander
Sep 24, 2010, 07:35 AM
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.

autorelease
Sep 24, 2010, 10:59 AM
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.

Sydde
Sep 24, 2010, 01:36 PM
ASCII ends with 127. What encoidng are you using for 128~255?

zippyfly
Sep 24, 2010, 11:07 PM
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!

Sander
Sep 25, 2010, 06:26 AM
Oh wow, I totally misunderstood. The Hex thing didn't have much to do with it; ignore my %x answer.