从 std::string & 获取 char 整数值std::wstring

发布于 2024-12-11 03:14:08 字数 555 浏览 0 评论 0原文

我试图通过在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只有影子陪我不离不弃 2024-12-18 03:14:08

msdn 文章说:

“输入字符串是可以解释的字符序列
作为指定类型的数值。该函数停止读取
输入字符串无法识别的第一个字符
数字的一部分。”

unicode 字符串测试代码,您将看到正确的输出:

printf("TEST 1: %d \n", _tstoi(_T("1")));

输出:

TEST 1: 1

就像 @Ylisar 所说, *toi 函数用于将数字值从字符串转换为整数变量。

下面的代码将输出数字表示形式,但请注意 const 变量的指针表示形式,以便您可以看到差异:

  printf("TEST 1: %d \n", _tstoi(_T("1")));
  printf("TEST a: %d \n", _tstoi(_T("a")));
  WCHAR* b(_T("b"));
  printf("TEST A: %d \n", _T("A"));
  printf("TEST b: %d \n", *b);

输出:

TEST 1: 1
TEST a: 0
TEST A: 13457492
TEST b: 98

http://msdn.microsoft.com/en-us /library/yd5xkb5c%28v=vs.80%29.aspx

如果您想总结(累积)这些值,我建议您查看STL 范围函数在此类事情上表现出色,例如

#include <numeric>
#include <string>

printf("TEST a: %d \n", *_T("a")); // 97
printf("TEST b: %d \n", *_T("b")); // 98

wstring uString(_T("ba"));
int result = accumulate(uString.begin(), uString.end(), 0);
printf("TEST accumulated: %d \n", result);

结果:

TEST a: 97
TEST b: 98
TEST accumulated: 195

这样您就不必使用 for 循环来遍历所有值。范围函数对于此类内容确实非常有用

。 href="http://www.sgi.com/tech/stl/accumulate.html" rel="nofollow">http://www.sgi.com/tech/stl/accumulate.html

The msdn article says:

"The input string is a sequence of characters that can be interpreted
as a numerical value of the specified type. The function stops reading
the input string at the first character that it cannot recognize as
part of a number."

If you test the code with unicode strings containing actual numbers, you'll see the correct output:

printf("TEST 1: %d \n", _tstoi(_T("1")));

output:

TEST 1: 1

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:

  printf("TEST 1: %d \n", _tstoi(_T("1")));
  printf("TEST a: %d \n", _tstoi(_T("a")));
  WCHAR* b(_T("b"));
  printf("TEST A: %d \n", _T("A"));
  printf("TEST b: %d \n", *b);

Output:

TEST 1: 1
TEST a: 0
TEST A: 13457492
TEST b: 98

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

#include <numeric>
#include <string>

printf("TEST a: %d \n", *_T("a")); // 97
printf("TEST b: %d \n", *_T("b")); // 98

wstring uString(_T("ba"));
int result = accumulate(uString.begin(), uString.end(), 0);
printf("TEST accumulated: %d \n", result);

Results:

TEST a: 97
TEST b: 98
TEST accumulated: 195

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

从﹋此江山别 2024-12-18 03:14:08

*toi 系列函数将字符串表示形式转换为整数表示形式,即“10”变为 10。您真正想要做的是根本不进行任何转换。将其更改为:

printf("TEST a: %d \n", _T('a'));
printf("TEST A: %d \n", _T('A'));
printf("TEST b: %d \n", _T('b'));

对于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:

printf("TEST a: %d \n", _T('a'));
printf("TEST A: %d \n", _T('A'));
printf("TEST b: %d \n", _T('b'));

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 ).

回眸一笑 2024-12-18 03:14:08

第一个问题,为什么 printf 不能按预期工作,Ylisar 已经回答了。关于对字符的十六进制表示求和的另一个问题稍微复杂一些。仅当给定字符串表示的数字(如“123”)转换为 123 时,使用 _tstoi() 函数从字符串到数字值的转换才会起作用。您想要的是字符表示形式的总和。

如果 Unicode 代码点低于 0x7F (0...127),则这只是 1 字节 UTF-8 表示形式的总和。然而,在使用 UNICODE 标志编译的 Windows 上,使用每个字符 2 字节的表示形式。在调试器中运行以下代码将释放此信息。

// ASCII 1 Byte per character
const char* letterA = "A";
int sumOfLetterA = letterA[0] + letterA[0]; // gives 130

// 2 Bytes per character (Windows)
const wchar_t* letterB = TEXT("B");
int sumOfLetterB = letterB[0] + letterB[0]; // gives 132

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.

// ASCII 1 Byte per character
const char* letterA = "A";
int sumOfLetterA = letterA[0] + letterA[0]; // gives 130

// 2 Bytes per character (Windows)
const wchar_t* letterB = TEXT("B");
int sumOfLetterB = letterB[0] + letterB[0]; // gives 132
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文