最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++實(shí)現(xiàn)字符格式相互轉(zhuǎn)換的示例代碼

 更新時(shí)間:2022年11月15日 11:20:19   作者:歐特克_Glodon  
這篇文章主要為大家詳細(xì)介紹了C++中實(shí)現(xiàn)字符格式相互轉(zhuǎn)換的方法,主要有UTF8與string互轉(zhuǎn)、wstring與string互轉(zhuǎn),感興趣的小伙伴可以了解一下

一、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 : 定義控制臺應(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;
}

簡單實(shí)用

CString strBuf;
int nSize = Unicode2UTF8(strBuf, NULL, 0);
char* szBuf = new char[nSize];
Unicode2UTF8(strBuf, szBuf, nSize);

以上就是C++實(shí)現(xiàn)字符格式相互轉(zhuǎn)換的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C++字符格式互換的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 詳解C語言中的#define宏定義命令用法

    詳解C語言中的#define宏定義命令用法

    有的時(shí)候?yàn)榱顺绦虻耐ㄓ眯?可以使用#define預(yù)處理宏定義命令,它的具體作用就是方便程序段的定義和修改,下面就來詳解C語言中的#define宏定義命令用法.
    2016-05-05
  • C++如何獲取系統(tǒng)信息 C++獲取IP地址、硬件信息等

    C++如何獲取系統(tǒng)信息 C++獲取IP地址、硬件信息等

    這篇文章主要為大家詳細(xì)介紹了C++如何獲取系統(tǒng)信,C++獲取IP地址、硬件信息等,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • C語言之關(guān)于二維數(shù)組在函數(shù)中的調(diào)用問題

    C語言之關(guān)于二維數(shù)組在函數(shù)中的調(diào)用問題

    這篇文章主要介紹了C語言之關(guān)于二維數(shù)組在函數(shù)中的調(diào)用問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • MFC實(shí)現(xiàn)字幕滾動(dòng)效果

    MFC實(shí)現(xiàn)字幕滾動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了MFC實(shí)現(xiàn)滾動(dòng)字幕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • 簡述C語言中system()函數(shù)與vfork()函數(shù)的使用方法

    簡述C語言中system()函數(shù)與vfork()函數(shù)的使用方法

    這篇文章主要介紹了簡述C語言中system()函數(shù)與vfork()函數(shù)的使用方法,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-08-08
  • Qt自定義控件實(shí)現(xiàn)多彩色儀表盤

    Qt自定義控件實(shí)現(xiàn)多彩色儀表盤

    這篇文章主要為大家詳細(xì)介紹了Qt自定義控件實(shí)現(xiàn)多彩色儀表盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • C語言寫一個(gè)散列表

    C語言寫一個(gè)散列表

    這篇文章主要介紹了C語言寫一個(gè)散列表,散列表,就是下標(biāo)可以為字母的數(shù)組。更多內(nèi)容和小編一起學(xué)習(xí)下面內(nèi)容吧
    2022-01-01
  • C語言中如何利用循環(huán)嵌套輸出一個(gè)菱形

    C語言中如何利用循環(huán)嵌套輸出一個(gè)菱形

    這篇文章主要介紹了C語言中如何利用循環(huán)嵌套輸出一個(gè)菱形問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • window調(diào)用api列出當(dāng)前所有進(jìn)程示例

    window調(diào)用api列出當(dāng)前所有進(jìn)程示例

    這篇文章主要介紹了window調(diào)用api列出當(dāng)前所有進(jìn)程示例,需要的朋友可以參考下
    2014-04-04
  • C語言枚舉與聯(lián)合圖文梳理講解

    C語言枚舉與聯(lián)合圖文梳理講解

    枚舉顧名思義就是把所有的可能性列舉出來,像一個(gè)星期分為七天我們就可以使用枚舉,聯(lián)合體是由關(guān)鍵字union和標(biāo)簽定義的,和枚舉是一樣的定義方式,不一樣的是,一個(gè)聯(lián)合體只有一塊內(nèi)存空間,什么意思呢,就相當(dāng)于只開辟最大的變量的內(nèi)存,其他的變量都在那個(gè)變量占據(jù)空間
    2023-01-01

最新評論

门源| 白朗县| 保亭| 墨玉县| 岑溪市| 纳雍县| 墨竹工卡县| 泾川县| 资源县| 桂阳县| 晋江市| 抚州市| 兴城市| 武隆县| 朔州市| 天峨县| 通化市| 西畴县| 定陶县| 西昌市| 通榆县| 吴川市| 酒泉市| 平武县| 阿荣旗| 上林县| 临猗县| 沾化县| 宁陕县| 平罗县| 盖州市| 定襄县| 措勤县| 台东市| 黔南| 文安县| 陆良县| 岑溪市| 宾阳县| 晴隆县| 名山县|