Reputation: 1254
I'm getting char* with latin letter print example : M\xe4da Primavesi
i'm trying to convert it to NSString , the final result should be Mäda Primavesi.
Anyone know how the conversation be done ?
Thanks
Upvotes: 0
Views: 780
Reputation: 17500
The encoding you want is NSISOLatin1StringEncoding
:
NSString *latin = [NSString stringWithCString:"M\xe4da Primavesi" encoding:NSISOLatin1StringEncoding];
BUT you will notice that this prints MÚ Primavesi
. That is because \x
is greedy, and interprets the "da"
as part of the hex \xe4da
. You have to find a way to separate the "\xe4"
part with the "da"
part.
This works:
NSString *latin = [NSString stringWithCString:"M\xe4""da Primavesi" encoding:NSISOLatin1StringEncoding]; // prints Mäda Primavesi
I suggest you encode your latin C-String using utf-8 string "M\u00e4da Primavesi"
instead, and decode it with NSUTF8StringEncoding
.
Upvotes: 1
Reputation: 2768
char *latinChars = "M\xe4da Primavesi";
NSString *chatStr = [NSString stringWithCString:latinChars encoding:NSASCIIStringEncoding];
NSLog(@"chatStr:%@", chatStr);
The result is:MÚ Primavesi
And I have a try:
char *latinChars = "M\xe4 da Primavesi"; //add an blank for 'da'
NSString *chatStr = [NSString stringWithCString:latinChars encoding:NSASCIIStringEncoding];
NSLog(@"chatStr:%@", chatStr);
The result is:Mä da Primavesi
Upvotes: 0
Reputation: 1505
Try the NSString
API stringWithCString:encoding:
as below,
`[NSString stringWithCString:cString encoding:NSUTF8StringEncoding];`
Upvotes: 0
Reputation: 3000
Look like latin1.
[NSString stringWithCString:cString encoding: NSISOLatin1StringEncoding]
Upvotes: 0