⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ccharcode.cpp

📁 pimserver是syncml_vc开发包的服务器实例
💻 CPP
字号:
#include "stdafx.h"
#include "ccharcode.h"

void UTF_8ToUnicode(wchar_t* pOut,char *pText)   
{  
    char* uchar = (char *)pOut;   
    uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);   
    uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);   
    return;   
}   
void UnicodeToUTF_8(char* pOut,wchar_t* pText)     
{   
    // 注意 WCHAR高低字的顺序,低字节在前,高字节在后   
    char* pchar = (char *)pText;   
    pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));
    pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);   
    pOut[2] = (0x80 | (pchar[0] & 0x3F));
    return;     
}   
void UnicodeToGB2312(char* pOut,wchar_t uData)   
{   
    WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(wchar_t),NULL,NULL);   
    return;   
}        
  
  
  
void GB2312ToUnicode(wchar_t* pOut,char *gbBuffer)   
{   
    ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);   
    return ;   
  
}   
 
  
void GB2312ToUTF_8(char* pOut,char *pText, int pLen)   
{   
    char buf[4];   
    int nLength = pLen* 3;   
    //char* rst = new char[nLength];   
    memset(buf,0,4);   
    memset(pOut,0,nLength);   
    int i = 0;   
    int j = 0;         
    while(i < pLen)   
    {   
            //如果是英文直接复制就可以   
            if( *(pText + i) >= 0)   
            {   
                    pOut[j++] = pText[i++];   
            }   
            else   
            {   
                wchar_t pbuffer;   
                GB2312ToUnicode(&pbuffer,pText+i);   
                UnicodeToUTF_8(buf,&pbuffer);   
                unsigned short int tmp = 0;   
                tmp = pOut[j] = buf[0];   
                tmp = pOut[j+1] = buf[1];   
                tmp = pOut[j+2] = buf[2];           
                j += 3;   
                i += 2;   
            }   
    }   
    pOut[j] = '\0';   
    //返回结果   
   // pOut = rst;                
    //delete []rst;      
    return;   
}   
  
 
void GB2312ToUTF_16(char* pOut,char *pText, int pLen)   
{   
    char buf[4];   
    int nLength = pLen* 2;   
    //char* rst = new char[nLength];   
    memset(buf,0,4);   
    memset(pOut,0,nLength);   
    int i = 0;   
    int j = 0;         
    while(i < pLen)   
    {   
            //如果是英文直接复制就可以   
            if( *(pText + i) >= 0)   
            {   
                    pOut[i++] = pText[i++];   
            }   
            else   
            {   
                wchar_t pbuffer;   
                GB2312ToUnicode(&pbuffer,pText+i);   
                //UnicodeToUTF_8(buf,&pbuffer);   
                unsigned short int tmp = 0;   
                tmp = pOut[i] = buf[0];   
                tmp = pOut[i+1] = buf[1];   
                //tmp = pOut[j+2] = buf[2];           
               // j += 2;   
                i += 2;   
            }   
    }   
    pOut[i] = '\0';   
    //返回结果   
   // pOut = rst;                
    //delete []rst;      
    return;   
}   
   
  
void UTF_8ToGB2312(char *pOut, char *pText, int pLen)   
  
{   
    //char * newBuf = new char[pLen];   
    char Ctemp[4];   
    memset(Ctemp,0,4);   
    int i =0;   
    int j = 0;   
    while(i < pLen)   
    {   
        if(pText[i] > 0)   
        {   
                pOut[j++] = pText[i++];                          
        }   
        else                    
        {   
                WCHAR Wtemp;   
                UTF_8ToUnicode(&Wtemp,pText + i);   
                UnicodeToGB2312(Ctemp,Wtemp);   
                pOut[j] = Ctemp[0];   
                pOut[j + 1] = Ctemp[1];   
                i += 3;       
                j += 2;      
        }   
    }   
    pOut[j] = '\0';   
    //pOut = newBuf;   
	//delete []newBuf;   
    return;    
  
}


void UTF_16ToGB2312(char *pOut, wchar_t *pText, int pLen)   
  
{   
    //char * newBuf = new char[pLen];   
    char Ctemp[4];   
    memset(Ctemp,0,4);   
    int i =0;   
    int j = 0;   
    while(i < pLen)   
    {   
        if(pText[i] > 0)   
        {   
                pOut[i++] = pText[i++];                          
        }   
        else                    
        {   
                WCHAR Wtemp;   
                //UTF_8ToUnicode(&Wtemp,pText + i);   
                UnicodeToGB2312(Ctemp,*(pText + i));   
                pOut[i] = Ctemp[0];   
                pOut[i + 1] = Ctemp[1];   
                i += 2;       
                //j += 2;      
        }   
    }   
    pOut[i] = '\0';   
    //pOut = newBuf;   
	//delete []newBuf;   
    return;    
  
}



   
//-------------------------------------------------------------------------------------
  //Description:
  // This function maps a character string to a wide-character (Unicode) string
  //
  //Parameters:
  // lpcszStr: [in] Pointer to the character string to be converted 
  // lpwszStr: [out] Pointer to a buffer that receives the translated string. 
  // dwSize: [in] Size of the buffer
  //
  //Return Values:
  // TRUE: Succeed
  // FALSE: Failed
  // 
  //Example:
  // MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
  //---------------------------------------------------------------------------------------
  BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
  {
    // Get the required size of the buffer that receives the Unicode 
    // string. 
    DWORD dwMinSize;
    dwMinSize = MultiByteToWideChar (CP_OEMCP, 0, lpcszStr, -1, NULL, 0);
  
    if(dwSize < dwMinSize)
    {
     return FALSE;
    }
  
    
    // Convert headers from ASCII to Unicode.
    return MultiByteToWideChar (CP_OEMCP, 0, lpcszStr, -1, lpwszStr, dwMinSize);  
    //return dwMinSize;
  }
  
  //-------------------------------------------------------------------------------------
  //Description:
  // This function maps a wide-character string to a new character string
  //
  //Parameters:
  // lpcwszStr: [in] Pointer to the character string to be converted 
  // lpszStr: [out] Pointer to a buffer that receives the translated string. 
  // dwSize: [in] Size of the buffer
  //
  //Return Values:
  // TRUE: Succeed
  // FALSE: Failed
  // 
  //Example:
  // MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
  //---------------------------------------------------------------------------------------
  BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
  {
   DWORD dwMinSize;
   dwMinSize = WideCharToMultiByte(CP_ACP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
   if(dwSize < dwMinSize)
   {
    return FALSE;
   }
  
   WideCharToMultiByte(CP_ACP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
    printf("mbyte %s\n",lpszStr);
   return TRUE;
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -