从 std::string & 获取 char 整数值std::wstring
我试图通过在 C++ WinAPI 中将每个字母的 int 值相加来将字符串转换为数字。所以在 ASCII 中; std::string "AA" 等于 130 (65+65)
该字符串可以是 std::string 或 std::wstring。
为什么无论我在其中输入什么字母,以下函数总是返回零值?难道它不应该返回字母的 ASCII 或 Unicode 整数值吗?
printf("TEST a: %d \n", _tstoi(_T("a")));
printf("TEST A: %d \n", _tstoi(_T("A")));
printf("TEST b: %d \n", _tstoi(_T("b")));
我的 VC++ 应用程序当前采用 Unicode,&前面的代码为每个字母打印出零。我记得听说 Unicode 与 ASCII 字符串非常不同,除了 Unicode 有一个大约 30,000 个字符库而 ASCII 是 256 个字符库(我认为是 256 个字符?)之外,你能澄清一下到底有什么不同吗? >
I am attempting to convert a string into a number by summing the int value of each letter together in C++ WinAPI. So in ASCII; the std::string "AA" would equal 130 (65+65)
The string can either be a std::string or an std::wstring.
Why does the following function always return the value of zero no matter what letter I put in it? Shouldn't it return either the ASCII or Unicode integer value of the letter?
printf("TEST a: %d \n", _tstoi(_T("a")));
printf("TEST A: %d \n", _tstoi(_T("A")));
printf("TEST b: %d \n", _tstoi(_T("b")));
My VC++ application is currently in Unicode, & the previous code prints out zero for each letter. I remember hearing that Unicode is very different to ASCII strings, can you clear up what exactly is different other than Unicode has a library of characters which is something like 30,000 long whilst ASCII is 256(I think?)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
msdn 文章说:
unicode 字符串测试代码,您将看到正确的输出:
输出:
就像 @Ylisar 所说, *toi 函数用于将数字值从字符串转换为整数变量。
下面的代码将输出数字表示形式,但请注意 const 变量的指针表示形式,以便您可以看到差异:
输出:
在 http://msdn.microsoft.com/en-us /library/yd5xkb5c%28v=vs.80%29.aspx
如果您想总结(累积)这些值,我建议您查看STL 范围函数在此类事情上表现出色,例如
结果:
这样您就不必使用 for 循环来遍历所有值。范围函数对于此类内容确实非常有用
。 href="http://www.sgi.com/tech/stl/accumulate.html" rel="nofollow">http://www.sgi.com/tech/stl/accumulate.html
The msdn article says:
If you test the code with unicode strings containing actual numbers, you'll see the correct output:
output:
Like @Ylisar said, the *toi functions are used to convert number values from strings to integer variables instead.
The following code will output the number representation instead, but watch out for the pointer representation of the const variables. I've left both versions so you can see the difference:
Output:
Check out more at http://msdn.microsoft.com/en-us/library/yd5xkb5c%28v=vs.80%29.aspx
If you want to sum up (accumulate) the values, I would recommend you checking out the STL range functions which does wonders on such things. For example
Results:
This way you don't have to have for-loops going through all the values. The range functions really are nice for stuff like this.
Check out more at: http://www.sgi.com/tech/stl/accumulate.html
*toi 系列函数将字符串表示形式转换为整数表示形式,即“10”变为 10。您真正想要做的是根本不进行任何转换。将其更改为:
对于unicode,底层表示取决于编码(例如非常流行的UTF-8,将LSB与ASCII表映射)。
the *toi family of functions converts a string representation to integer representation, that is, "10" becomes 10. What you actually want to do is no conversion at all. Change it to:
As for unicode, the underlying representation depends on the encoding ( for example UTF-8, which is very popular, maps the LSB with the ASCII table ).
第一个问题,为什么 printf 不能按预期工作,Ylisar 已经回答了。关于对字符的十六进制表示求和的另一个问题稍微复杂一些。仅当给定字符串表示的数字(如“123”)转换为 123 时,使用 _tstoi() 函数从字符串到数字值的转换才会起作用。您想要的是字符表示形式的总和。
如果 Unicode 代码点低于 0x7F (0...127),则这只是 1 字节 UTF-8 表示形式的总和。然而,在使用 UNICODE 标志编译的 Windows 上,使用每个字符 2 字节的表示形式。在调试器中运行以下代码将释放此信息。
The first question, why printf does not work as intened has already been answered by Ylisar. The other question about summing the hexadecimal representation of a character is a little more complex. The conversion from strings to number values with the _tstoi() function will only work if the given string represents a number like "123" gets converted to 123. What you want is the sum of the characters representation.
In case of Unicode code points below 0x7F (0...127) this is simply the sum of the 1 Byte UTF-8 representation. However on Windows compiled with UNICODE flag a 2 Byte per character representation is used. Running the following code in the debugger will releal this.