CString 和其他类型字符串的转换

MSDN 链接

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();
}

第二种

使用 wstringwstringstream 取代 stringstringstream


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();
}

备注

  1. CW2A CT2A CA2W CA2T 等的意思
    C 可能是:change 的缩写 ,W 是Unicode 字符,A 是 ANSI 字符。2 因为英文读作:two ,与 to 同音,于是 CW2A 的意思是把 Unicode 字符转换成 ANSI 字符。其中 T 代表自动根据环境确定是什么类型的字符

  2. CString 对象可以直接赋值给 stringwstring 对象,但是不能反过来

  3. 因为 CString 不支持 string 对象给他赋值,于是要是用 c_str 函数,这个函数的作用是返回一个字符串的首地址,即常规的 C 字符数组类型的字符串。

  4. 也可以使用 wstringwstringstream ,这两个都是 Unicode 字符的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值