简体   繁体   English

Objective-C字符编码-将char更改为int,然后返回

[英]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): 我得到了:(在Java中完美 - 我为你处理编码):

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. 我想在UTF-8中做所有事情,但是不知道如何获取char的UTF-8值。 And if I've got it, how to change that value back to an char . 如果我知道了,如何将其值改回char
The NSLog in the code outputs the correct values. 代码中的NSLog输出正确的值。 But when I try to do the opposite with the algorithm (Ie - 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). NSString适用于2字节长(16位)的unichar字符。 Char is one byte long so you can only store code point from U+0000 to U+00FF (ie Basic Latin and Latin-1 Supplement). 字符长度为一个字节,因此您只能存储从U+0000U+00FF代码点(即Basic Latin和Latin-1 Supplement)。

You should do you math on unichar values then use +[NSString stringWithCharacters:length:] to create the string representation. 你应该对unichar值进行数学运算,然后使用+[NSString stringWithCharacters:length:]来创建字符串表示。

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. 您的代码可能会在U+D800U+DFFF之间生成无效的Unicode字符的代码点。 The standard reserves them to encode code points from U+10000 to U+10FFFF in UTF-16 by pairs of 16-bit code units. 该标准保留它们通过成对的16位代码单元将代码点从U+10000U+10FFFF以UTF-16编码。 In such a case, your string would be ill-formed and could neither be displayed nor converted in UTF8. 在这种情况下,您的字符串将格式错误,无法在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 . 另外,临时变量tmp2没用,并且在连接字符串时不应创建新的newString,而应使用NSMutableString

I am assuming that your strings are NSStrings consisting of numerals which represent a number. 我假设您的字符串是由代表数字的数字组成的NSStrings。 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);

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? 不知道这是否是您的意思,但是据我了解,您想使用char的UTF-8字符串编码创建NSString吗?

If that's what you want, maybe you can use the initWithCString:encoding: method in NSString. 如果这是你想要的,也许你可以在NSString中使用initWithCString:encoding:方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM