首页 > 代码库 > 16进制和汉字之间的互转!《转》

16进制和汉字之间的互转!《转》

/****************************16进制转汉字***********************************/
AnsiString TForm1::Hex_To_Chinese(AnsiString Hex)
{
        //中英文数字参杂在一起也可以转换
        AnsiString ret;
        for(int i=1; i<Hex.Length()+1; i += 2)
        {
                AnsiString TempStr = "0x" + Hex.SubString(i,2);
                ret += AnsiString((char)(TempStr.ToInt()));
        }
        return ret;
}
/****************************16进制转汉字***********************************/

/****************************汉字转16进制***********************************/
AnsiString TForm1::Chinese_To_Hex(char* P_BCD, int iLen)
{
        //中英文数字参杂在一起也可以转换
        AnsiString ret = "";
        char hChar, lChar, Temp;
        for (int i=0; i<iLen; i++)
                {
                Temp = *P_BCD++;
                hChar = (Temp&0xF0)>>4;
                lChar = (Temp&0x0F);
                if ((hChar >= 0x00)&&(hChar <= 0x09))
                        hChar += 0x30;
                if ((hChar >= 0x0A)&&(hChar <= 0x0F))
                        hChar += 0x37;
                if ((lChar >= 0x00)&&(lChar <= 0x09))
                        lChar += 0x30;
                if ((lChar >= 0x0A)&&(lChar <= 0x0F))
                        lChar += 0x37;
                ret += AnsiString(hChar);
                ret += AnsiString(lChar);
        }
        return ret;
}
/****************************汉字转16进制***********************************/

void __fastcall TForm1::Button4Click(TObject *Sender)
{
        String strChinese = "中国共浐党万岁";       

   //D6D0B9FAB9B29BBAB5B3CDF2CBEA 此段为汉字的机内码
        String strTemp = Chinese_To_Hex(strChinese.c_str(), strChinese.Length());
        //Chinese_To_Hex()此函数转的是机内码 不是GB码

    ShowMessage(strTemp);
        strTemp = Hex_To_Chinese(strTemp);
        ShowMessage(strTemp);
};

16进制和汉字之间的互转!《转》