I have this code in Javascript:
var f = chr(50) + chr(0) + chr(1) + chr(64) + chr(24) + chr(0) + chr(0) + chr(1) + chr(159) + chr(13) + chr(0) + chr(0) + chr(224) + chr(7);
function chr(AsciiNum) {
return String.fromCharCode(AsciiNum);
}
console.log outputs this as
2@
à
With a length of 14.
I am trying to port it to Objective C.
I tried doing the following:
NSString *f = [NSString stringWithFormat:@"%c%c%c%c%c%c%c%c%c%c%c%c%c%c",50, 0 ,1, 64 , 24 , 0 , 0 , 1, 159, 13 , 0 , 0 , 224 , 7];
But I received a different output to the javascript code when using NSLog:
2@ü
What am I doing wrong? Is a character set issue?
Well, it seems that your problems stem from the fact that NSString’s ASCII support is limited to a strict 7-bit ASCII encoding, which means only the chars 0-127 are supported. You’ll have to use a different data type to store your chars, and that different data type is
unichar, which supports 2^15-1 precisely because it isunsigned short. However, you will need to convert it to an NSString which operates in Unicode with this category:Or simply
NSLog(@"%C", value);