将CString转换为无符号字符 [英] Conversion of CString to unsigned char

查看:73
本文介绍了将CString转换为无符号字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将CString变量转换为无符号字符.
任何人都可以帮忙.

这是我的代码,

Hi,

I want to convert the CString variable to unsigned char.
Can anyone help in this.

Here is my code,

VARIANT varVal;
CString strValue = "1";
unsigned char* cVal = (unsigned char*) (LPCTSTR) strValue;
varVal.bVal = *cVal;



如果我执行上述代码,则该值显示为"49"(varVal.bVal),但我想显示为"1".



If I execute the above code the value is displaying as "49"(varVal.bVal), but i want to display as "1".

Any help in this?

推荐答案

首先,您需要了解为什么得到49.这是因为这是字符"1"的ASCII值. ':
http://en.wikipedia.org/wiki/ASCII [ http://www.cplusplus.com/reference/clibrary/cstdlib/atoi [ ^ ].

该函数需要一个以空字符结尾的字符串(作为char *指针),它看起来像是一个类型转换,但实际上这是一个强制转换运算符:
http://msdn.microsoft.com/en-us/library/aa300569%28v = vs.60%29.aspx [ ^ ].

-SA
First of all, you need to understand why you get 49. This is because this is the ASCII value of the character ''1'':
http://en.wikipedia.org/wiki/ASCII[^].

The whole idea of "conversion" is wrong and manifest the lack of your understanding how data is represented in principle. To get what you want, you need to do something which is not related to "conversion"; this is "parsing". One of the functions you can use is this:
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi[^].

This function needs a null-terminated string (as a char * pointer), which only looks like a typecast, but in fact this is a casting operator:
http://msdn.microsoft.com/en-us/library/aa300569%28v=vs.60%29.aspx[^].

—SA


CString strValue ="1";
unsigend char * cVal =(unsigned char *)strValue.GetBuffer(0);
CString strValue = "1";
unsigend char* cVal = (unsigned char *) strValue.GetBuffer(0);


上面可以修改为,
The above can be modified as,
varVal.vt = VT_BSTR;
varVal.bstrVal = m_strValue.AllocSysString();
VariantChangeType(&varVal,&varVal,0,VT_UI1);



现在varVal.bVal的值为"1".



Now the varVal.bVal will have the value "1".


这篇关于将CString转换为无符号字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆