欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解C/C++實(shí)現(xiàn)各種字符轉(zhuǎn)換方法合集

 更新時(shí)間:2022年09月27日 16:20:17   作者:坐望云起  
這篇文章主要為大家詳細(xì)介紹了C/C++中實(shí)現(xiàn)各種字符轉(zhuǎn)換的方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C++具有一定借鑒價(jià)值,需要的可以參考一下

一、std::string 和 std::wstring 互轉(zhuǎn)

1、直接聲明std::wstring

std::wstring uriImage{L"D:\\Project\\screen.jpg"};
Uri uri{ uriImage };

2、wstring_convert

由于C++17之后取消std::codecvt_utf8的支持,所以這個(gè)方法不是最好的??梢钥紤]使用WideCharToMultiByte和MultiByteToWideChar替代。

std::wstring wideString = std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(stringToConvert);

3、WideCharToMultiByte和MultiByteToWideChar

std::string MyTools::ConvertWideToANSI(const std::wstring& wstr)
{
    int count = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.length(), NULL, 0, NULL, NULL);
    std::string str(count, 0);
    WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, &str[0], count, NULL, NULL);
    return str;
}
 
std::wstring MyTools::ConvertAnsiToWide(const std::string& str)
{
    int count = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), NULL, 0);
    std::wstring wstr(count, 0);
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), &wstr[0], count);
    return wstr;
}
 
std::string MyTools::ConvertWideToUtf8(const std::wstring& wstr)
{
    int count = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.length(), NULL, 0, NULL, NULL);
    std::string str(count, 0);
    WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &str[0], count, NULL, NULL);
    return str;
}
 
std::wstring MyTools::ConvertUtf8ToWide(const std::string& str)
{
    int count = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), NULL, 0);
    std::wstring wstr(count, 0);
    MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), &wstr[0], count);
    return wstr;
}

二、winrt::hstring 和 std::string 互轉(zhuǎn)

winrt::hstring是C++/WinRT 的自定義字符串類(lèi)型(在 C++/WinRT 基礎(chǔ)庫(kù)中定義,即%WindowsSdkDir%Include\<WindowsTargetPlatformVersion>\cppwinrt\winrt\base.h)。

// 直接聲明一個(gè)winrt::hstring
winrt::hstring w{L"zh-CN"};
// 調(diào)用winrt::to_hstring方法直接把std::string轉(zhuǎn)為winrt::hstring
winrt::hstring htr = winrt::to_hstring("this is a std string");
 
// 調(diào)用winrt::to_string轉(zhuǎn)為std::string
winrt::hstring w{L"zh-CN1111測(cè)試"};
std::string temp = winrt::to_string(w);

三、const char* 和 char* 互轉(zhuǎn)

1、const char*轉(zhuǎn)char*

const char *filePath= "abc";
char *newPath = const_cast<char *>(filePath);

2、 char*轉(zhuǎn)const char*

直接賦值即可

const char* cpc;
char* pc = "abcde";
cpc = pc;

四、QString 和 std::string 互轉(zhuǎn)

std::string a = "asdfg";
QString path = QString::fromStdString("asdfg");
std::string d = path.toStdString();

補(bǔ)充

CString 和 *char 的轉(zhuǎn)換

1:CString -> *char

1)CString轉(zhuǎn)化為*char可以使用CString中的GetBuffer()函數(shù),具體如下:

CString string1 = _T("string");
char *str = string1.GetBuffer();

注意的是,在GetBuffer后要使用ReleaseBuffer以更新對(duì)象內(nèi)部數(shù)據(jù),否則會(huì)發(fā)生不可意料的意外。

2)可以使用強(qiáng)制轉(zhuǎn)換。

CString string1 = _T(“string”);
char *str = (LPTSTR)(LPCTSTR)string1;

3)也可使用函數(shù)strcpy實(shí)現(xiàn)轉(zhuǎn)換。

4)使用CString的GetAt()函數(shù):

CString string1 = _T("string");
char *str = string1.GetAt(0);

即獲取下標(biāo)為0的字符。

2:*char -> CString

1)使用format函數(shù):

char *str = "string";
CString string1;
string1.format("%s",str);

2)同樣也可以強(qiáng)制轉(zhuǎn)換:

char *str = "string";
CString string1(str);

*char 與 int 的轉(zhuǎn)換

1:*char -> int

1)使用atoi()函數(shù):

char *val = "12345";
int num = atoi(val);

2:int -> *char

1)使用itoa()函數(shù):

int num = 12345;
char buf[5];
itoa(buf, num, 10);

itoa()函數(shù)中后面10代表十進(jìn)制。

2)使用sprintf()函數(shù):

int num = 12345;
char buf[6];
sprintf(buf, "%d", num);

到此這篇關(guān)于詳解C/C++實(shí)現(xiàn)各種字符轉(zhuǎn)換方法合集的文章就介紹到這了,更多相關(guān)C++字符轉(zhuǎn)換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • c++ STL庫(kù)容器之集合set代碼實(shí)例

    c++ STL庫(kù)容器之集合set代碼實(shí)例

    在本篇文章里小編給大家整理的是關(guān)于c++STL庫(kù)容器之集合set代碼實(shí)例,需要的朋友們可以參考下。
    2020-03-03
  • 詳解Bucket Sort桶排序算法及C++代碼實(shí)現(xiàn)示例

    詳解Bucket Sort桶排序算法及C++代碼實(shí)現(xiàn)示例

    桶排序是一種線(xiàn)性排序算法,這里我們來(lái)詳解Bucket Sort桶排序算法及C++代碼實(shí)現(xiàn)示例,需要的朋友可以參考下
    2016-07-07
  • Qt超時(shí)鎖屏的實(shí)現(xiàn)示例

    Qt超時(shí)鎖屏的實(shí)現(xiàn)示例

    本文主要介紹了Qt超時(shí)鎖屏的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C語(yǔ)言版掃雷小游戲

    C語(yǔ)言版掃雷小游戲

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言版的掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • c語(yǔ)言:基于函數(shù)指針的兩個(gè)示例分析

    c語(yǔ)言:基于函數(shù)指針的兩個(gè)示例分析

    本篇文章是對(duì)c語(yǔ)言中函數(shù)指針的兩個(gè)示例做了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • Visual Studio 2019創(chuàng)建C++ Hello World項(xiàng)目的方法

    Visual Studio 2019創(chuàng)建C++ Hello World項(xiàng)目的方法

    這篇文章主要介紹了Visual Studio 2019創(chuàng)建C++ Hello World項(xiàng)目的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • C++中的STL常用算法之遍歷算法詳解

    C++中的STL常用算法之遍歷算法詳解

    這篇文章主要介紹了C++中的STL常用算法之遍歷算法詳解,ransform() 可以將函數(shù)應(yīng)用到容器的元素上,并將這個(gè)函數(shù)返回的值保存到另一個(gè)容器中,它返回的迭代器指向輸出容器所保存的最后一個(gè)元素的下一個(gè)位置,需要的朋友可以參考下
    2023-12-12
  • C++類(lèi)中如何使用定義的類(lèi)型別名

    C++類(lèi)中如何使用定義的類(lèi)型別名

    這篇文章主要介紹了C++類(lèi)中如何使用定義的類(lèi)型別名,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C++11中std::future的具體使用方法

    C++11中std::future的具體使用方法

    這篇文章主要介紹了C++11中std::future的具體使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • VC實(shí)現(xiàn)獲取當(dāng)前正在運(yùn)行的進(jìn)程

    VC實(shí)現(xiàn)獲取當(dāng)前正在運(yùn)行的進(jìn)程

    這篇文章主要介紹了VC實(shí)現(xiàn)獲取當(dāng)前正在運(yùn)行的進(jìn)程,涉及VC針對(duì)系統(tǒng)進(jìn)程的相關(guān)操作技巧,需要的朋友可以參考下
    2015-05-05

最新評(píng)論