參考鏈接:https://blog.csdn.net/yanchao7788/article/details/53196901
參考鏈接:你真的懂 Unicode 和 UTF-8 是什么關(guān)系嗎?來看看這個就徹底懂了!_魔都飄雪的博客-CSDN博客_utf8和unicode的關(guān)系
UTF-8沒辦法直接轉(zhuǎn)GBK,必須先轉(zhuǎn)成unicode,再轉(zhuǎn)成GBK
1.把UTF-8轉(zhuǎn)成unicode,根據(jù)參考的鏈接,我做了實現(xiàn)。文章來源:http://www.zghlxwxcb.cn/news/detail-508240.html
2.把unicode轉(zhuǎn)成GBK,這里的代碼是FATFS文件系統(tǒng)里的,可以自己取官網(wǎng)下,里面的數(shù)組太長了,自己去下吧。文章來源地址http://www.zghlxwxcb.cn/news/detail-508240.html
typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
typedef unsigned char BYTE; /* char must be 8-bit */
typedef unsigned short WORD; /* 16-bit unsigned integer */
typedef unsigned int DWORD; /* 32-bit unsigned integer */
typedef WORD WCHAR; /* UTF-16 character type */
void ff_utf8touni(BYTE *inputS,//input string
WORD sLength,//string's length
DWORD *outputArr,//output Arr
WORD arrLength)//output arr max length
{
WORD i,j,k,t,outputIndex,num;
DWORD data;
BYTE ch;
outputIndex = 0;
for(i=0;i<arrLength;i++)
{
outputArr[i] = 0;
}
if(sLength == 0 || arrLength == 0)return;
for(i=0;i<sLength;)
{
ch = inputS[i];
if(ch & 0X80)
{
num = 0;
for(j=0;j<6;j++)//計算總共多少個個數(shù)
{
if(ch&0X80)
{
num++;
ch <<= 1;
}
else
{
break;
}
}
if(i+num > sLength)break;
k = 8 - (num+1);
t=0;
for(j=0;j<k;j++)
{
t <<= 1;
t |= 1;
}
data = inputS[i] & t;//第一個字節(jié)
for(j=1;j<num;j++)//其他的字節(jié)
{
data <<= 6;
data |= inputS[i+j]&0X3F;
}
outputArr[outputIndex++] = data;
i+=num;
}
else
{
outputArr[outputIndex++] = ch;
i++;
}
if(outputIndex >= arrLength)break;
}
}
WCHAR ff_uni2oem ( /* Returns OEM code character, zero on error */
DWORD uni /* UTF-16 encoded character to be converted */
)
{
const WCHAR *p;
WCHAR c = 0, uc;
UINT i, n, li, hi;
if (uni < 0x80) { /* ASCII? */
c = (WCHAR)uni;
} else { /* Non-ASCII */
if (uni < 0x10000) { /* Is it in BMP? */
uc = (WCHAR)uni;
p = 0;
/* DBCS */
//switch (cp) { /* Get conversion table */
//case 932 : p = uni2oem932; hi = sizeof uni2oem932 / 4 - 1; break;
//case 936 : p = uni2oem936; hi = sizeof uni2oem936 / 4 - 1; break;
//case 949 : p = uni2oem949; hi = sizeof uni2oem949 / 4 - 1; break;
//case 950 : p = uni2oem950; hi = sizeof uni2oem950 / 4 - 1; break;
//}
p = uni2oem936; hi = sizeof uni2oem936 / 4 - 1;
if (p) { /* Is it valid code page? */
li = 0;
for (n = 16; n; n--) { /* Find OEM code */
i = li + (hi - li) / 2;
if (uc == p[i * 2]) break;
if (uc > p[i * 2]) {
li = i;
} else {
hi = i;
}
}
if (n != 0) c = p[i * 2 + 1];
}
}
}
return c;
}
到了這里,關(guān)于UTF-8轉(zhuǎn)GBK的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!