Philipp Lugovoy
Philipp Lugovoy

Reputation: 1

How convert string utf-8?

i've an NSString like this:

NSString *word = @"119,111,114,100"

So, what i want to do is to convert this NSString to word

So the question is, in which way can i convert a string to a word?

Upvotes: 0

Views: 177

Answers (3)

user2485710
user2485710

Reputation: 9801

libicu it's an UTF8 library that supports a conversion from an array of bytes as stated here.

The thing is, it offers Java, C or C++ APIs, not obj-c.

Upvotes: 0

Martin R
Martin R

Reputation: 539685

// I have added some values to your sample input :-)
NSString *word = @"119,111,114,100,32,240,159,145,141";

// Separate components into array:
NSArray *array = [word componentsSeparatedByString:@","];

// Create NSData containing the bytes:
NSMutableData *data = [[NSMutableData alloc] initWithLength:[array count]];
uint8_t *bytes = [data mutableBytes];
for (NSUInteger i = 0; i < [array count]; i++) {
    bytes[i] = [array[i] intValue];
}

// Convert to NSString (interpreting the bytes as UTF-8):
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"%@", str);

Output:

word 👍

Upvotes: 1

Anoop Vaidya
Anoop Vaidya

Reputation: 46533

Try this:

NSString *word = @"119,111,114,100";
NSArray *array=[word componentsSeparatedByString:@","];
for (NSString *string in array) {
   char character=[string integerValue];
    NSLog(@"%c",character);    
}

Output:

w
o
r
d

Upvotes: 0

Related Questions