ibm123
ibm123

Reputation: 1254

Objective c unicode char* to NSString

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

Answers (4)

John Estropia
John Estropia

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

simalone
simalone

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

IronMan
IronMan

Reputation: 1505

Try the NSString API stringWithCString:encoding: as below,

`[NSString stringWithCString:cString encoding:NSUTF8StringEncoding];`

Upvotes: 0

user3125367
user3125367

Reputation: 3000

Look like latin1.

[NSString stringWithCString:cString encoding: NSISOLatin1StringEncoding]

Upvotes: 0

Related Questions