C++的TCHAR字符串和char字符串互转
下面用一个例子来演示一下TCHAR和char字符串的互转,主要用到两个函数WideCharToMultiByte和MultiByteToWideChar。
观察下这两个函数,WideChar表示宽字节的字符串,MultiByte表示窄字节的字符串,TCHAR是宽字节,char是窄字节,所以TCHAR到char用WideCharToMultiByte函数,反之则用MultiByteToWideChar函数。
如下所示,将TCHAR的字符串转化为char,第一次调用返回一个字符串的长度,这个长度是变换后新的字符串所占的需要的内存空间的长度,第二次调用则是将字符串进行转化。
TCHAR *a = _TEXT”txt,txt,t”);//返回字符串的长度int size = WideCharToMultiByteCP_ACP, 0, a, -1, NULL, 0, NULL, false);//申请一个多字节的字符串变量char* str = new char[sizeofchar) * size];//将STR转成strWideCharToMultiByteCP_ACP, 0, a, -1, str, size, NULL, false);
如下所示,将char转换为TCHAR,同上。
//返回字符串的长度int T_sizey = MultiByteToWideCharCP_ACP, 0, ftostry, -1, NULL, 0);//申请一个多字节的字符串变量TCHAR *stry = new TCHAR[sizeofwchar_t) * T_sizey];MultiByteToWideCharCP_ACP, 0, ftostry, -1, stry, T_sizey);
下面是完成的代码,实现了一个输入TCHAR的字符串作为文件路径,然后解析为char,再将文件内的内容从float转换char再转换为TCHAR字符串并返回的过程
#include <iostream>#include <tchar.h>#include <string>#include <stdio.h>#include <windows.h>#include <wchar.h>#include <atlconv.h>#include <fstream>float path_x[301], path_y[301];//读取txt文件中的路径数据bool getPathDatachar *pathname) {std::ifstream openfile;//打开文件openfile.openpathname);char line[80];int i = 0;//获取每一行数据while openfile.getlineline, 80)){char *p, *p1, *p2;char *buf;//用逗号分离x-y的坐标p = strtok_sline, “,”, &buf);p1 = p;p = strtok_sNULL, “,”, &buf);p2 = p;//char*转换为floatfloat f1 = atofp1);float f2 = atofp2);//将float类型的轨迹数据存入数组path_x[i] = f1;path_y[i] = f2;i++;}openfile.close);return true;}TCHAR* testTCHAR *a) {//返回字符串的长度int size = WideCharToMultiByteCP_ACP, 0, a, -1, NULL, 0, NULL, false);//申请一个多字节的字符串变量char* str = new char[sizeofchar) * size];//将STR转成strWideCharToMultiByteCP_ACP, 0, a, -1, str, size, NULL, false);std::cout << str << std::endl;char *pathname;char *anglename;char *steername;char *p;char *buf;//用逗号分离路径p = strtok_sstr, “,”, &buf);pathname = p;std::cout << pathname << std::endl;p = strtok_sNULL, “,”, &buf);anglename = p;std::cout << anglename << std::endl;p = strtok_sNULL, “,”, &buf);steername = p;std::cout << steername << std::endl;int dataSize = 6 * 200 + 2 * 2;TCHAR *reData = new TCHAR[dataSize + 1];reData[0] = _T’\0′);getPathDatapathname);wcscatreData, _TEXT”1,”));wcscatreData, _TEXT”2,”));for int i = 0; i < 100; i++) {//std::cout << path_x[i] << “,” << path_y[i] << std::endl;char ftostrx[7] = { “\0” };_gcvtpath_x[i], 4, ftostrx);char sepx[] = “,”;strcatftostrx, sepx);//std::cout << ftostrx << std::endl;int T_sizex = MultiByteToWideCharCP_ACP, 0, ftostrx, -1, NULL, 0);//std::cout << T_sizex << std::endl;TCHAR *strx = new TCHAR[sizeofwchar_t) * T_sizex];MultiByteToWideCharCP_ACP, 0, ftostrx, -1, strx, T_sizex);//std::wcout << strx << std::endl;wcscatreData, strx);char ftostry[7] = { “\0” };_gcvtpath_y[i], 4, ftostry);char sepy[] = “,”;strcatftostry, se快三最准高手教学atapathname);wcscatreData, _TEXT”1,”));wcscatreData, _TEXT”2,”));for int i = 0; i < 100; i++) {//std::cout << path_x[i] << “,” << path_y[i] << std::endl;char ftostrx[7] = { “\0” };_gcvtpath_x[i], 4, ftostrx);char sepx[] = “,”;strcatftostrx, sepx);//std::cout << ftostrx << std::endl;int T_sizex = MultiByteToWideCharCP_ACP, 0, ftostrx, -1, NULL, 0);//std::cout << T_sizex << std::endl;TCHAR *strx = new TCHAR[sizeofwchar_t) * T_sizex];MultiByteToWideCharCP_ACP, 0, ftostrx, -1, strx, T_sizex);//std::wcout << strx << std::endl;wcscatreData, strx);char ftostry[7] = { “\0” };_gcvtpath_y[i], 4, ftostry);char sepy[] = “,”;strcatftostry, sepy);//std::cout << ftostry << std::endl;int T_sizey = MultiByteToWideCharCP_ACP, 0, ftostry, -1, NULL, 0);//std::cout << T_sizey << std::endl;TCHAR *stry = new TCHAR[sizeofwchar_t) * T_sizey];MultiByteToWideCharCP_ACP, 0, ftostry, -1, stry, T_sizey);std::wcout << stry << std::endl;wcscatreData, stry);}std::cout << wcslenreData) << std::endl;std::wcout << reData << std::endl;return reData;}int main){TCHAR a1[] = _TEXT”E:\\python\\python\\2021\\202105\\2105131\\path_data.txt,E:\\python\\python\\2021\\202105\\2105131\\cars_angle.txt,E:\\python\\python\\2021\\202105\\2105131\\steer_angle.txt”);TCHAR *a = a1;TCHAR* c = testa); std::cout << “Hello World!\n”;}