سؤال

After reading the documents about NSString, it seems that NSStrings may be 7 or 8 bits (although judging by the comments, that may be completely wrong). I'm very new to Objective-C and running into some syntax issues. This is my string currently:

NSString *charles = [[NSString stringWithFormat:@"%c", i] encoding:NSUTF8StringEncoding];

The error that I'm getting is "No known instance method for selector "encoding"". Does anyone know the proper way to convert an NSString from 7 bit to 8 bit?

Update:

unichar value;
    for (value = 0; value < 255; value++)  {
[_device writeToTerminal:[NSString stringWithFormat:@"%C", value]];

}

Now, 0-127 print perfectly. The output on the terminal is as expected: 01,02.....A,B,C... ... 1A,1B,1C etc. But, once I hit above 127 (printed as 7F), I get the value "C2 80" when it should just simply be "80."

Also, I apologize for whatever I did wrong in my question to warrant 3 down votes. Could someone explain what I did wrong so that I can avoid that in the future?

هل كانت مفيدة؟

المحلول 2

To create an NSString containing one character with a given Unicode in the range 0 .. 0xFFFF you can either use the %C format:

unichar c = ...;
NSString *charles = [NSString stringWithFormat:@"%C", c];

or use stringWithCharacters:

unichar c = ...;
NSString *charles = [NSString stringWithCharacters:&c length:1];

To get the original character code back:

unichar c = [charles characterAtIndex:0];

نصائح أخرى

You want to grab the C string:

NSString *charles = [NSString stringWithFormat:@"%c", i];
[charles UTF8String];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top