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.
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+0000toU+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+D800andU+DFFFthat aren’t valid Unicode characters. The standard reserves them to encode code points fromU+10000toU+10FFFFin 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.