类型转换:
MFC工程使用utf-8格式的时候,CString格式:_T(“”)/L””
1.std::string -> Cstring
①CString cs = string.c_str() //有的版本可以,有的版本const char* = string.c_str()
②Ⅰ.CString = CA2T(string.c_str());
Ⅱ.CA2T cs(string.c_str());
Cstring = (LPCTSTR)cs;//这里必须要转换一次
2.Cstring -> std::string
①std::string std = (CstringA)CString;
②std::string std = (CT2A)Cstring;
3.unsigned short -> std::string
char puf[20];
unsigned short us;
itoa(us, puf, 16);//将unsigned short转换成字符后是16进制形式的字符返回给字符数组puf (unsigned short = 0x01,转化之后字符puf仍然是0x01的形式)
参数16也可以写其他进制,转换之后的字符就是对应进制的形式。us也可以换成int类型
string query = puf;//字符数组可直接赋值给std::string
4.std::string ->unsigned short
const char* cw1 = std::string.c_str();
unsigned short us = strtol(cw1, NULL, 16);//该方法用法和itoa有些类似
5.std::string -> double
string s = " 123 ";
double n = atof(s.c_str());
字符串拼接:
1.CString中Format函数与格式输入与输出:(有两种形式,一种两个参数,另一种三个参数,主要区别为三个参数的是线程安全的并不多用)
CString
CString cs;
int p;
cs.Format(_T("AAA%d"), p);//p会替换掉%d
完整概貌:
格式化字符串forma("%d",12)意思是将一个整形的格式化的字符(我认为是保持其形状不变)
1).格式说明总是以%字符开始,以下是不同类型数据的格式方式%号后的说明:
d输出带符号十进制数,bool类型也可用d,true输出1,false输出0
o输出无符号八进制数
x输出无符号十六进制数
u输出无符号数
c输出单个字符
s输出一串字符
f输出实数(6位小数)
e以指数形式输出实数
g选用f与e格式中输出宽度较小的格式,不输出0
ld输入输出long型数据
lf输入输出double型数据
m数据输出宽度为m
.n输出小数位数为n
.2f保留小数点后2位小数,适用于double类型,2可以修改为其他数字