網(wǎng)站首頁 編程語言 正文
一、UTF8轉(zhuǎn)std:string
std::string UTF8_To_string(const std::string& str)
{
int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
wchar_t* pwBuf = new wchar_t[nwLen + 1]; //一定要加1,不然會(huì)出現(xiàn)尾巴
memset(pwBuf, 0, nwLen * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char* pBuf = new char[nLen + 1];
memset(pBuf, 0, nLen + 1);
WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string strRet = pBuf;
delete[]pBuf;
delete[]pwBuf;
pBuf = NULL;
pwBuf = NULL;
return strRet;
}
二、string_To_UTF8
std::string string_To_UTF8(const std::string& str)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t* pwBuf = new wchar_t[nwLen + 1]; //一定要加1,不然會(huì)出現(xiàn)尾巴
ZeroMemory(pwBuf, nwLen * 2 + 2);
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char* pBuf = new char[nLen + 1];
ZeroMemory(pBuf, nLen + 1);
::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string strRet(pBuf);
delete[]pwBuf;
delete[]pBuf;
pwBuf = NULL;
pBuf = NULL;
return strRet;
}
三、wstring轉(zhuǎn)string
std::string wstring2string(std::wstring wstr)
{
string result;
int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
if (len <= 0)return result;
char* buffer = new char[len + 1];
if (buffer == NULL)return result;
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
buffer[len] = '\0';
result.append(buffer);
delete[] buffer;
return result;
}
四、string轉(zhuǎn)wstring
std::wstring string2wstring(std::string str)
{
wstring result;
int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
if (len < 0)return result;
wchar_t* buffer = new wchar_t[len + 1];
if (buffer == NULL)return result;
MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
buffer[len] = '\0';
result.append(buffer);
delete[] buffer;
return result;
}
五、unicode屬性和多字節(jié)屬性轉(zhuǎn)換(char轉(zhuǎn)wchar_t)
// WideChar.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <atlbase.h>
//#include <comutil.h>
#include <atlstr.h>
using namespace std;
//方法1:MultiByteToWideChar, WideCharToMultiByte
void Way_1()
{
//char轉(zhuǎn)wchar_t
char *p1 = "abc";
wchar_t p2[20] = L"EFG";
MultiByteToWideChar(CP_ACP, 0, p1, strlen(p1) + 1, p2, sizeof(p2));
printf("%S\n", p2);
//wchar_t轉(zhuǎn)char
wchar_t *p3= L"EFG";
char p4[20];
WideCharToMultiByte(CP_ACP, 0, p3, -1, p4, sizeof(p4), NULL, NULL);
printf("%s\n", p4);
}
//方法2:A2W, W2A, T2A, A2T
void Way_2()
{
//需要添加頭文件 <atlbase.h>
char * p1 = "abc";
wchar_t *p2 = L"def";
TCHAR *P3 = _T("CC");
USES_CONVERSION;
wchar_t *p5 = A2W(p1);
char * p4 = T2A(P3);
}
方法3:
//void Way_3()
//{//需要添加頭文件<comutil.h>,一般在MFC工程下使用
// CString str = "abc";//只能存一種
// _bstr_t bstr = "abc";//可以用非unicode
// bstr += L"efg";//可以用unicode
// char *p = bstr;
// wchar_t *p2 = bstr;
//}
int main()
{
Way_1();
//Way_2();
return 0;
}
六、Unicode ansi utf8轉(zhuǎn)換
int Unicode2UTF8(const wchar_t* pUnicode, char* pUTF8Buffer, int nBufferSize)
{
if( (nBufferSize == 0) && (pUTF8Buffer != NULL) )
return 0;
int result = WideCharToMultiByte(CP_UTF8, NULL, pUnicode, -1, pUTF8Buffer, nBufferSize, NULL, NULL);
if ((result > 0) && (pUTF8Buffer != NULL))
pUTF8Buffer[result-1] = 0;
return result;
}
int UTF82Unicode(const char* pUTF8, wchar_t* pUnicodeBuffer, int nBufferSize)
{
if( (nBufferSize == 0) && (pUnicodeBuffer != NULL) )
return 0;
int result = MultiByteToWideChar(CP_UTF8, NULL, pUTF8, -1, pUnicodeBuffer, nBufferSize);
if ((result > 0) && (pUnicodeBuffer != NULL))
pUnicodeBuffer[result-1] = 0;
return result;
}
int Unicode2Ansi(const wchar_t* pUnicode, char* pAnsiBuffer, int nBufferSize)
{
if( (nBufferSize == 0) && (pAnsiBuffer != NULL) )
return 0;
int result = ::WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, pAnsiBuffer, nBufferSize, NULL, NULL);
if ((result > 0) && (pAnsiBuffer != NULL))
pAnsiBuffer[result-1] = 0;
return result;
}
int Ansi2Unicode(const char* pAnsi, wchar_t* pUnicodeBuffer, int nBufferSize)
{
if( (nBufferSize == 0) && (pUnicodeBuffer != NULL) )
return 0;
int result = ::MultiByteToWideChar(CP_ACP, 0, pAnsi, -1, pUnicodeBuffer, nBufferSize);
if ((result > 0) && (pUnicodeBuffer != NULL))
pUnicodeBuffer[result-1] = 0;
return result;
}
簡(jiǎn)單實(shí)用
CString strBuf;
int nSize = Unicode2UTF8(strBuf, NULL, 0);
char* szBuf = new char[nSize];
Unicode2UTF8(strBuf, szBuf, nSize);
原文鏈接:https://blog.csdn.net/m0_37251750/article/details/108376157
相關(guān)推薦
- 2022-05-04 Python的五個(gè)標(biāo)準(zhǔn)數(shù)據(jù)類型你認(rèn)識(shí)幾個(gè)_python
- 2022-07-20 nginx?添加http_stub_status_module模塊_nginx
- 2022-04-10 Blazor頁面組件用法介紹_基礎(chǔ)應(yīng)用
- 2022-05-17 EdgeX 設(shè)備服務(wù)與core-data、core-command的交互
- 2022-04-16 C++中allocator類使用示例_C 語言
- 2022-06-29 在Oracle中使用正則表達(dá)式_oracle
- 2022-10-15 Python?UnicodedecodeError編碼問題解決方法匯總_python
- 2022-05-02 ASP.NET?Core中間件實(shí)現(xiàn)限流的代碼_實(shí)用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支