把 CString
赋值给 std::string
第一种
把 CString
强制转换成 CStringA
class 十进制转二进制
{
private:
unsigned int temp = 0;//临时存放变量的无符号整型,用来给bitset使用
public:
CString strinput;//用户输入的字符串
CString stroutput;//输出结果的字符串
void 转换();
};
void 十进制转二进制::转换()
{
string tempstr = CStringA(strinput);
stringstream str1;
str1 << tempstr;
str1 >> temp;
stringstream str2;
str2 << bitset<32>(temp);
string str;
str2 >> str;
stroutput = str.c_str();
}
第二种
使用 wstring
和 wstringstream
取代 string
和 stringstream
void 十进制转二进制::转换()
{
wstring strtemp = strinput;
wstringstream str1;
str1 << strtemp;
str1 >> temp;
wstringstream str2;
str2 << bitset<32>(temp);
wstring str;
str2 >> str;
stroutput = str.c_str();
}
第三种
使用 CT2A
,不知道这东西是不是函数
void 十进制转二进制::转换()
{
string strtemp = CT2A(strinput);
stringstream str1;
str1 << strtemp;
str1 >> temp;
stringstream str2;
str2 << bitset<32>(temp);
string str;
str2 >> str;
stroutput = str.c_str();
}
第四种
使用 CW2A
void 十进制转二进制::转换()
{
string strtemp = CW2A(strinput);
stringstream str1;
str1 << strtemp;
str1 >> temp;
stringstream str2;
str2 << bitset<32>(temp);
string str;
str2 >> str;
stroutput = str.c_str();
}
备注
-
CW2A
CT2A
CA2W
CA2T
等的意思
C 可能是:change 的缩写 ,W 是Unicode 字符,A 是 ANSI 字符。2 因为英文读作:two ,与 to 同音,于是CW2A
的意思是把 Unicode 字符转换成 ANSI 字符。其中 T 代表自动根据环境确定是什么类型的字符 -
CString
对象可以直接赋值给string
或wstring
对象,但是不能反过来 -
因为
CString
不支持string
对象给他赋值,于是要是用c_str
函数,这个函数的作用是返回一个字符串的首地址,即常规的 C 字符数组类型的字符串。 -
也可以使用
wstring
和wstringstream
,这两个都是 Unicode 字符的