網站首頁 編程語言 正文
一、std::string 和 std::wstring 互轉
1、直接聲明std::wstring
std::wstring uriImage{L"D:\\Project\\screen.jpg"};
Uri uri{ uriImage };
2、wstring_convert
由于C++17之后取消std::codecvt_utf8的支持,所以這個方法不是最好的。可以考慮使用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 互轉
winrt::hstring是C++/WinRT 的自定義字符串類型(在 C++/WinRT 基礎庫中定義,即%WindowsSdkDir%Include\<WindowsTargetPlatformVersion>\cppwinrt\winrt\base.h)。
// 直接聲明一個winrt::hstring
winrt::hstring w{L"zh-CN"};
// 調用winrt::to_hstring方法直接把std::string轉為winrt::hstring
winrt::hstring htr = winrt::to_hstring("this is a std string");
// 調用winrt::to_string轉為std::string
winrt::hstring w{L"zh-CN1111測試"};
std::string temp = winrt::to_string(w);
三、const char* 和 char* 互轉
1、const char*轉char*
const char *filePath= "abc";
char *newPath = const_cast<char *>(filePath);
2、 char*轉const char*
直接賦值即可
const char* cpc;
char* pc = "abcde";
cpc = pc;
四、QString 和 std::string 互轉
std::string a = "asdfg";
QString path = QString::fromStdString("asdfg");
std::string d = path.toStdString();
補充
CString 和 *char 的轉換
1:CString -> *char
1)CString轉化為*char可以使用CString中的GetBuffer()函數,具體如下:
CString string1 = _T("string");
char *str = string1.GetBuffer();
注意的是,在GetBuffer后要使用ReleaseBuffer以更新對象內部數據,否則會發生不可意料的意外。
2)可以使用強制轉換。
CString string1 = _T(“string”);
char *str = (LPTSTR)(LPCTSTR)string1;
3)也可使用函數strcpy實現轉換。
4)使用CString的GetAt()函數:
CString string1 = _T("string");
char *str = string1.GetAt(0);
即獲取下標為0的字符。
2:*char -> CString
1)使用format函數:
char *str = "string";
CString string1;
string1.format("%s",str);
2)同樣也可以強制轉換:
char *str = "string";
CString string1(str);
*char 與 int 的轉換
1:*char -> int
1)使用atoi()函數:
char *val = "12345";
int num = atoi(val);
2:int -> *char
1)使用itoa()函數:
int num = 12345;
char buf[5];
itoa(buf, num, 10);
itoa()函數中后面10代表十進制。
2)使用sprintf()函數:
int num = 12345;
char buf[6];
sprintf(buf, "%d", num);
原文鏈接:https://blog.csdn.net/bashendixie5/article/details/127058337
相關推薦
- 2022-12-23 利用Python實現文件讀取與輸入以及數據存儲與讀取的常用命令_python
- 2022-12-01 django第一個項目127.0.0.1:8000不能訪問的解決方案詳析_python
- 2024-04-02 Centos無法獲取IP報No suitable device found for this con
- 2022-04-12 Git:解決Git向碼云中push文件報錯:! [rejected] master -
- 2022-04-22 Object.assign()是深拷貝還是淺拷貝
- 2022-10-22 BroadcastReceiver靜態注冊案例詳解_Android
- 2022-07-25 python數據清洗中的時間格式化實現_python
- 2021-12-02 C++內存分布及用法_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支