Pangolin
Pangolin

Reputation: 7444

Objective-C character encoding - Change char to int, and back

Simple task: I need to convert two characters to two numbers, add them together and change that back to an character.
What I have got: (works perfect in Java - where encoding is handled for you, I guess):

int myChar1 = (int)([myText1 characterAtIndex:i]);
int myChar2 = (int)([myText2 characterAtIndex:keyCurrent]);
int newChar = (myChar1 + myChar2);
//NSLog(@"Int's %d, %d, %d", textChar, keyChar, newChar);
char newC = ((char) newChar);

NSString *tmp1 = [NSString stringWithFormat:@"%c", newC];
NSString *tmp2 = [NSString stringWithFormat:@"%@", newString];
newString = [NSString stringWithFormat:@"%@%@", tmp2, tmp1]; //Adding these char's in a string

The algorithm is perfect, but now I can't figure out how to implement encoding properties. I would like to do everything in UTF-8 but have no idea how to get a char's UTF-8 value, for instance. And if I've got it, how to change that value back to an char.
The NSLog in the code outputs the correct values. But when I try to do the opposite with the algorithm (I.e. - the values) then it goes wrong. It gets the wrong character value for weird/odd characters.

Upvotes: 1

Views: 2110

Answers (3)

TheAmateurProgrammer
TheAmateurProgrammer

Reputation: 9392

Not sure if this is what you meant, but from what I understood, you wanted to create a NSString with the UTF-8 string encoding from a char?

If that's what you want, maybe you can use the initWithCString:encoding: method in NSString.

Upvotes: 0

Nicolas Bachschmidt
Nicolas Bachschmidt

Reputation: 6505

NSString works with unichar characters that are 2 bytes long (16 bits). Char is one byte long so you can only store code point from U+0000 to U+00FF (i.e. Basic Latin and Latin-1 Supplement).

You should do you math on unichar values then use +[NSString stringWithCharacters:length:] to create the string representation.

But there is still an issue with that solution. You code may generate code points between U+D800 and U+DFFF that aren't valid Unicode characters. The standard reserves them to encode code points from U+10000 to U+10FFFF in UTF-16 by pairs of 16-bit code units. In such a case, your string would be ill-formed and could neither be displayed nor converted in UTF8.

Also, the temporary variable tmp2 is useless and you should not create a new newString as you concatenate the string but rather use a NSMutableString.

Upvotes: 2

David M. Syzdek
David M. Syzdek

Reputation: 15788

I am assuming that your strings are NSStrings consisting of numerals which represent a number. If that is the case, you could try the following:

Include the following headers:

#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>

Then use the following code:

// convert NSString to UTF8 string
const char * utf8String1 = [myText1 UTF8String];
const char * utf8String2 = [myText2 UTF8String];

// convert UTF8 string into long integers
long num1 = strtol(utf8String1, NULL 0);
long num2 = strtol(utf8String2, NULL 0);

// perform calculations
long calc = num1 - num2;

// convert calculated value back into NSString
NSString * calcText = [[NSString alloc] initWithFormat:@"%li" calc];

// convert calculated value back into UTF8 string
char calcUTF8[64];
snprintf(calcUTF8, 64, "%li", calc);

// log results
NSLog(@"calcText: %@", calcText);
NSLog(@"calcUTF8: %s", calcUTF8);

Upvotes: 1

Related Questions