views:

18

answers:

2

How do I get my byte array to a string that handles the Swedish letters å ä ö?

I do this at the moment: NSString *hexString = [NSString stringWithFormat:@"%c", resData[i]];

(resData contains ascii characters) But when I use the hexString in my output label I get jibberish for all non English chars. How can I make a selected byte to an encode that handles the Swedish characters?

Regards Daniel

A: 

I don't get it.. from NSString reference you have:

- (id)initWithBytes:(const void *)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding

so you should just do

[[NSString alloc] initWithBytes:resData 
                  length:yourLength
                  encoding:NSUTF8StringEncoding]
Jack
but if I do it like that, how do I get a specific resData? I know the length of the whole resData array. for example, I want to get resData[i] and show it as a string.
KungWaz
you mean you want to convert just a character?
Jack
yes, only one char in the byte array.
KungWaz
That wouldn't work. If your array is a byte array then many bytes will represent a single character in UTF8. You should first convert the whole array to a string and then extract single characters.. UTF8 has variable sized character so just getting the i-th wouldn't work
Jack
but if one byte represents one char then it is that char I need, and want to make it not jibberischy. Need to take one specific byte and turn it to utf8 or anything that can show å ä ö
KungWaz
as i already told you for example the angstrom has U+00C5 encoding in UTF8, this is 2 bytes not just one. You can't obtain it from just reading one byte each.
Jack
Its that I get my data from reading socket, didReadData, and one byte is one char, but with a oem charset. I have made a switch now as a fast solution where I hax out all åäöÅÄÖ, but will try the stated ones from you to get it better. Thanks for the fast replyes =)
KungWaz
A: 

resData does NOT contain ASCII characters, because ASCII does not include characters for å, ä, and ö. If resData is a UTF-8 encoded string, then some characters (including these) are encoded using more than one byte.

It looks like you want a way to print the *n*th character. First, do as @Jack said and turn those bytes into an NSString:

/* Assuming |resData| is NULL-terminated and UTF-8 encoded... */
NSString *text = [NSString stringWithUTF8String:resData];

Now, to print the *n*th character of text:

NSUInteger n = 3;
NSString *character = [text substringWithRange:NSMakeRange(n, 1)];
NSLog(@"character #%d: %@", n, character);

You can also set character as the string value of a control. It should display just fine.

The key take-away: byte[i] is NOT necessarily the same thing as the *i*th character. Text is always encoded, and you must always be (painfully) aware of that.

Jeremy W. Sherman