今天用到了Sqlite,由于它内部是使用UTF-8编码,所以在Windows应用中出现了乱码。
简单的搜索了一下,相互转换的方法很多,我觉得比较好的,是
http://www.vckbase.com/document/viewdoc/?id=1444我稍微改进了一下:
static WCHAR* UTF82Unicode(WCHAR* pBuffer,char *pSource, int buff_size)
{
int i, j, max;
char* uchar = (char *)pBuffer;
max = buff_size - 2;
for(i = 0, j = 0; pSource[j] != '\0'; i += 2, j += 3)
{
if (i > max) {
break;
}
uchar[i+1] = ((pSource[j] & 0x0F) << 4) + ((pSource[j+1] >> 2) & 0x0F);
uchar[i] = ((pSource[j+1] & 0x03) << 6) + (pSource[j+2] & 0x3F);
}
uchar[i] = '\0';
uchar[i+1] = '\0';
return pBuffer;
}
在Windows中的话,还有更简单的方法完成转换:
比如从UTF-8到Unicode:
WCHAR buff[255];
MultiByteToWideChar(CP_UTF8, 0, argv[i], -1, buff, sizeof(buff));
item.name = W2A(buff);
argv[i]是要转换的字节数组