<分区>
<分区>
这是我的字符数组返回的内容:
char *methodChars = (char *) sqlite3_column_text(statement, 3);
(char *) methodChars = 0x090f5e00 "Start cooking pasta in a large pan of boiling water.\xa0 Meanwhile, spray a large pan with oil and heat.\xa0\xa0 Add the onion and celery and cook over medium heat for 3 minutes or until soft.\xa0 Stir in the corn kernels.\xa0 Put the cornflour into a small bowl and gradually add 80 ml (1/3 cup) milk, stirring until smooth.\xa0 Add the remaining milk and the stock to the pan then gradually add the cornflour mixture, stirring constantly.\xa0 Keep stirring until the sauce boils and thickens.\xa0 Remove from heat and stir in the cheese until melted.\xa0 When the pasta is al dente, drain and add with the tuna to the pan.\xa0 Stir to combine, and serve immediately."
这是我转换它的地方,这是我得到的:
NSString *method = [[NSString alloc] initWithUTF8String:methodChars];
这是我从方法中得到的:
nil
谁能解释一下原因,以及我该如何解决这个问题?
当 methodChars 不包含 \xa0
而只有一个空格时,此代码适用于某些情况。谁能解释为什么这可能是一个原因?
最佳答案
您的字符串未以 UTF-8 编码:它包含字节 \xa0
。如果字符串应该包含 U+00A0 不间断空格字符,则它应该被编码为 \xc2\xa0
。
您可以使用 \xa0
映射到有效字符的编码来创建字符串,例如:
NSString *method = [[NSString alloc] initWithCString:methodChars encoding:NSISOLatin1StringEncoding];
关于ios - 为什么将 char 数组转换为字符串会创建一个 nil 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19259335/