中文字符集在ARM上的移植关键点在于CodePage,从Linux源代码中找到中文CodePage表,就可以利用CodePage来进行字符转换。中文CodePage号为936。找到文件nls_cp936.cpp
关键的字符寻表算法如下:
/*Unicode转换为ASCII*/
static int uni2char(const wchar_t uni, unsigned char *out, int boundlen)
{
unsigned char *uni2charset;
unsigned char cl = uni&0xFF;
unsigned char ch = (uni>>8)&0xFF;
int n;
if (boundlen <= 0)
{
return 0;
}
uni2charset = (unsigned char*)page_uni2charset[ch];
if (uni2charset)
{
if (boundlen <= 1)
return 0;
out[0] = uni2charset[cl*2];
out[1] = uni2charset[cl*2+1];
if (out[0] == 0x00 && out[1] == 0x00)
return 0;
n = 2;
}
else if (ch==0 && cl)
{
out[0] = cl;
n = 1;
}
else
return 0;
return n;
}
/*ASCII转换为 Unicode*/
static int char2uni(const unsigned char *rawstring, int boundlen, wchar_t *uni)
{
unsigned char ch, cl;
wchar_t *charset2uni;
int n;
if (boundlen <= 0)
return 0;
if (boundlen == 1)
{
*uni = rawstring[0];
return 1;
}
ch = rawstring[0];
cl = rawstring[1];
charset2uni = (wchar_t*)page_charset2uni[ch];
if (charset2uni && cl)
{
*uni = charset2uni[cl];
if (*uni == 0x0000) return 0;
n = 2;
}
else
{
*uni = ch;
n = 1;
}
return n;
}