字符集之间转换

来源:互联网 发布:华为整体网络解决方案 编辑:程序博客网 时间:2024/06/07 11:18


const char*utf8ToAnsi(constchar*src)

{

  intnLen = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)src, -1, NULL, 0);

 

    WCHAR * wszANSI = new WCHAR[nLen+1];

    memset(wszANSI, 0, nLen*2+2);

    nLen =MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)src, -1, wszANSI, nLen);

    nLen =WideCharToMultiByte(CP_ACP, 0, wszANSI, -1, NULL, 0, NULL, NULL);

    char*szUTF8 = new char[nLen+1];

    memset(szUTF8, 0, nLen+1);

    WideCharToMultiByte(CP_ACP, 0,wszANSI, -1, szUTF8, nLen, NULL, NULL);

 

  delete[]wszANSI;

  returnszUTF8;

}

 

const char*ansiToUtf8(constchar*src)

{

  intnLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)src, -1, NULL,0); 

 

    WCHAR * wszANSI = new WCHAR[nLen+1];

    memset(wszANSI, 0, nLen*2+2);

    nLen =MultiByteToWideChar(CP_ACP, 0, (LPCSTR)src, -1, wszANSI, nLen);       

nLen = WideCharToMultiByte(CP_UTF8, 0, wszANSI, -1, NULL, 0, NULL,NULL); 

char *szUTF8 = new char[nLen+1];

    memset(szUTF8, 0, nLen+1);

    WideCharToMultiByte (CP_UTF8, 0,wszANSI, -1, szUTF8, nLen, NULL, NULL);

  delete[]wszANSI;

  returnszUTF8;

}

 

const char*wideToAnsi(constwchar_t*src)

{

    intnLen = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);

    char*szAnsi = new char[nLen+1];

    memset(szAnsi, 0, nLen+1);

    WideCharToMultiByte (CP_ACP, 0,src, -1, szAnsi, nLen, NULL, NULL);

 

  returnszAnsi;

}

 

std::string wideToAnsi_v2(const wchar_t* src)

{

   

  intnLen = WideCharToMultiByte(CP_ACP, 0, src, -1, NULL, 0, NULL, NULL);

  char*szAnsi = new char[nLen+1];

  memset(szAnsi, 0, nLen+1);

  WideCharToMultiByte (CP_ACP, 0,src, -1, szAnsi, nLen, NULL, NULL);

 

  string str(szAnsi);

  delete[]szAnsi;

  szAnsi = NULL;

 

  returnstr;

}

 

const char*wideToUtf8(constwchar_t*src)

{

 

    intnLen = WideCharToMultiByte(CP_UTF8, 0, src, -1, NULL, 0, NULL, NULL);

    char*szUTF8 = new char[nLen+1];

    memset(szUTF8, 0, nLen+1);

    WideCharToMultiByte (CP_UTF8, 0,src, -1, szUTF8, nLen, NULL, NULL);

  returnszUTF8;

}

 

std::string wideToUtf8_v2(const wchar_t* src)

{

   

  intnLen = WideCharToMultiByte(CP_UTF8, 0, src, -1, NULL, 0, NULL, NULL);

  char*szUTF8 = new char[nLen+ 1];

  memset(szUTF8, 0, nLen + 1);

  WideCharToMultiByte (CP_UTF8, 0,src, -1, szUTF8, nLen, NULL, NULL);

 

  std::string str(szUTF8);

  delete[]szUTF8;

  szUTF8 = NULL;

  returnstr;

}

 

const wchar_t*utf8ToWide(constchar*src)

{

 

  intnLen = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)src, -1, NULL, 0);

 

    WCHAR * wszANSI = new WCHAR[nLen+1];

    memset(wszANSI, 0, nLen*2+2);

    MultiByteToWideChar(CP_UTF8, 0,(LPCSTR)src, -1, wszANSI, nLen);    

 

  returnwszANSI;

}

 

const wchar_t*ansiToWide(constchar*src)

{

 

  intnLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (LPCSTR)src, -1, NULL,0); 

 

    WCHAR* wszANSI = new WCHAR[nLen+1];

    memset(wszANSI, 0, nLen*2+2);

 

    MultiByteToWideChar(CP_ACP, 0,(LPCSTR)src, -1, wszANSI, nLen);

 

  returnwszANSI;

}

0 0
原创粉丝点击