简体   繁体   English

遍历包含“扩展ASCII”字符的const无符号char *

[英]Iterating through const unsigned char * containing “Extended ASCII” characters

In my sqlite database for an iPhone app, I encode/compress a long array of integers (up to 5 digits) into a string using the extended ASCII character set to get it down to 2 characters. 在我的iPhone应用程序的sqlite数据库中,我使用扩展的ASCII字符集将一长串整数(最多5位数字)编码/压缩为字符串,以将其缩减为2个字符。 (In other words, I encode it using base150) (换句话说,我使用base150对其进行编码)

When getting it out of the database, sqlite3_column_text() returns the string as a "const unsigned char *". 当将其从数据库中删除时,sqlite3_column_text()将字符串作为“ const unsigned char *”返回。 I can print this string using printf correctly (it shows even the ASCII characters over 128 properly) but when I try to iterate through it and access each character of the string individually to convert back into my integers, characters with ASCII values over 128 fail, because they're multibyte and it's only getting one byte (I think). 我可以使用printf正确打印此字符串(它甚至可以正确显示128个以上的ASCII字符),但是当我尝试对其进行迭代并分别访问该字符串的每个字符以转换回我的整数时,ASCII值超过128的字符将失败,因为它们是多字节的,而且只有一个字节(我认为)。

Example: 例:

I have this string called encodedString which contains: svÖ) 我有一个名为encodeString的字符串,其中包含:svÖ)

unsigned char c = encodedString[0];
unsigned char d = encodedString[2];

printf("%c", c);  //outputs "s"
printf("%c", d);  //outputs "\303"
printf("%s", encodedString);  //outputs "svÖ)"

I've also tried wchar_t with the same results. 我也尝试过wchar_t,结果相同。 I have gotten it to work using NSStrings, but it's very slow, and I'm doing this many thousands of times (NSMakeRange is the culprit according to the profiler), so I want it to be as fast as possible, hence C. 我已经使用NSStrings使其工作了,但是它非常慢,而且我已经做了数千次了(根据探查器,NSMakeRange是罪魁祸首),所以我希望它尽可能快,因此是C。

What's the trick to getting a single multibyte/extended ASCII character out of a string? 从字符串中获取单个多字节/扩展ASCII字符的诀窍是什么?

Rather than using a TEXT column, I would recommend using a BLOB column where the data contains an array of integers of whatever size you want to use (perhaps 16-bit unsigned). 我建议不要使用TEXT列,而建议使用BLOB列,该列中的数据包含要使用的任何大小的整数数组(也许是16位无符号)。

You can use sqlite_column_bytes() to determine the size of the column, allowing for variable-length columns to be used. 您可以使用sqlite_column_bytes()确定列的大小,从而允许使用可变长度的列。

This will avoid the complexity you are currently facing. 这样可以避免您当前面临的复杂性。

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

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