VC to achieve GB2312 BIG5 Unicode code conversion methods

  • 2020-04-02 02:30:53
  • OfStack

This article mainly discusses the method of string and file encoding conversion under VC compilation environment in the form of an example. Specific methods are as follows:

One, file encoding format conversion


//GB2312 code file converted to Unicode:
if((file_handle = fopen(filenam,"rb")) != NULL)
{
    //Reads the buffer in binary form from the GB2312 source file
    numread = fread(str_buf_pool,sizeof(char),POOL_BUFF_SIZE,file_handle);
    fclose(file_handle);
   //GB2312 file buffer converts to UNICODE
   nLen  =MultiByteToWideChar(CP_ACP,0,str_buf_pool,-1,NULL,0); 
   MultiByteToWideChar(CP_ACP,0,str_buf_pool,-1,(LPWSTR)str_unicode_buf_pool,nLen);
   //Assemble UNICODE Little Endian file file header identifier "0xFF 0xFE"
   //Note: UNICODE Big Endian file header identifier "0xFF 0xFE"
   //The difference between Little Endian and Big Endian codes is not detailed here
   unicode_little_file_header[0]=0xFF;
   unicode_little_file_header[1]=0xFE;
   //Store the target file
   if((file_handle=fopen(filenewname,"wb+")) != NULL)
   {
   fwrite(unicode_little_file_header,sizeof(char),2,file_handle);
   numwrite = fwrite(str_unicode_buf_pool,sizeof(LPWSTR),nLen,file_handle);
   fclose(file_handle);
   }
}

Two, string encoding format conversion


//GB2312 convert to Unicode:
wchar_t* GB2312ToUnicode(const char* szGBString)
{
    UINT nCodePage = 936; //GB2312
    int nLength=MultiByteToWideChar(nCodePage,0,szGBString,-1,NULL,0);
    wchar_t* pBuffer = new wchar_t[nLength+1];
    MultiByteToWideChar(nCodePage,0,szGBString,-1,pBuffer,nLength);
    pBuffer[nLength]=0;
    return pBuffer;
}
//BIG5 converts to Unicode:
wchar_t* BIG5ToUnicode(const char* szBIG5String)
{
    UINT nCodePage = 950; //BIG5
    int nLength=MultiByteToWideChar(nCodePage,0,szBIG5String,-1,NULL,0);
    wchar_t* pBuffer = new wchar_t[nLength+1];
    MultiByteToWideChar(nCodePage,0,szBIG5String,-1,pBuffer,nLength);
    pBuffer[nLength]=0;
    return pBuffer;
}
//Unicode conversion to GB2312:
char* UnicodeToGB2312(const wchar_t* szUnicodeString)
{
    UINT nCodePage = 936; //GB2312
    int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL);
    char* pBuffer=new char[nLength+1];
    WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL);
    pBuffer[nLength]=0;
    return pBuffer;
}
//Unicode to BIG5:
char* UnicodeToBIG5(const wchar_t* szUnicodeString)
{
    UINT nCodePage = 950; //BIG5
    int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL);
    char* pBuffer=new char[nLength+1];
    WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL);
    pBuffer[nLength]=0;
    return pBuffer;
}
//Traditional Chinese BIG5 converted to simplified Chinese GB2312
char* BIG5ToGB2312(const char* szBIG5String)
{
    LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC);
    wchar_t* szUnicodeBuff = BIG5ToUnicode(szBIG5String);
    char* szGB2312Buff = UnicodeToGB2312(szUnicodeBuff);
    int nLength = LCMapString(lcid,LCMAP_SIMPLIFIED_CHINESE, szGB2312Buff,-1,NULL,0);
    char* pBuffer = new char[nLength + 1];
    LCMapString(0x0804,LCMAP_SIMPLIFIED_CHINESE,szGB2312Buff,-1,pBuffer,nLength);
    pBuffer[nLength] = 0;
    
    delete[] szUnicodeBuff;
    delete[] szGB2312Buff;
    return pBuffer;
}
//Simplified Chinese GB2312 converted to traditional Chinese BIG5
char* GB2312ToBIG5(const char* szGBString)
{
    LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC);
    int nLength = LCMapString(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,NULL,0);
    char* pBuffer=new char[nLength+1];
    LCMapString(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,pBuffer,nLength);
    pBuffer[nLength]=0;
    wchar_t* pUnicodeBuff = GB2312ToUnicode(pBuffer);
    char* pBIG5Buff = UnicodeToBIG5(pUnicodeBuff);
    delete[] pBuffer;
    delete[] pUnicodeBuff;
    return pBIG5Buff;
}

API function: MultiByteToWideChar parameter description

The first parameter is the code page, and the GetLocaleInfo function is used to get the code page of the current system, 936: simplified Chinese, 950: traditional Chinese
The second parameter is an option, which is usually 0
The third argument is the address of the ANSI string, which is the ANSI string (AnsiString) of the language specified in the first argument.
The fourth parameter is the length of the ANSI string, and if -1 is used, it indicates that the string ends with 0
The fifth parameter is the address of the converted unicode string (WideString), which, if NULL, represents the length of the generated string
The sixth parameter is the capacity of the converted unicode string cache, that is, the number of unicode characters.


Related articles: