wchar_t*轉換string


場景

wchar[]轉換string

實現代碼

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>
 
// wchar_t to string
void Wchar_tToString(std::string& szDst, wchar_t *wchar)
{
    wchar_t * wText = wchar;
    DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的運用
    char *psText; // psText為char*的臨時數組,作為賦值給std::string的中間變量
    psText = new char[dwNum];
    WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次運用
    szDst = psText;// std::string賦值
    delete []psText;// psText的清除
}

調用

	// 存放當前程序運行路徑
	WCHAR SelfFile[MAX_PATH];
	//獲取當前進程路徑
	GetModuleFileName(NULL, SelfFile, MAX_PATH);
    // 當前程序存放路徑
	string Current_Path;
	Wchar_tToString(Current_Path,SelfFile);

參考

STRING轉WCHAR 和WCHAR 轉STRING

https://blog.csdn.net/sinat_35261315/article/details/72636712


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM