如何从字符中检索整数值?

发布于 2024-09-06 16:29:53 字数 1202 浏览 4 评论 0原文

如何将字符转换为整数值?例如,我一直尝试将“A”读作1,将“​​B”读作2,依此类推。我尝试将字符与字母表中的每个字母进行比较并返回适当的值。

int intvalue(char letter)
{
if(letter == "A")
    return 1;
else if(letter == "B")
    return 2;
else if(letter == "C")
    return 3;
else if(letter == "D")
    return 4;
else if(letter == "E")
    return 5;
else if(letter == "F")
    return 6;
else if(letter == "G")
    return 7;
else if(letter == "H")
    return 8;
else if(letter == "I")
    return 9;
else if(letter == "J")
    return 10;
else if(letter == "K")
    return 11;
else if(letter == "L")
    return 12;
else if(letter == "M")
    return 13;
else if(letter == "N")
    return 14;
else if(letter == "O")
    return 15;
else if(letter == "P")
    return 16;
else if(letter == "Q")
    return 17;
else if(letter == "R")
    return 18;
else if(letter == "S")
    return 19;
else if(letter == "T")
    return 20;
else if(letter == "U")
    return 21;
else if(letter == "V")
    return 22;
else if(letter == "W")
    return 23;
else if(letter == "X")
    return 24;
else if(letter == "Y")
    return 25;
else if(letter == "Z")
    return 26;

收到“错误:ISO C++ 禁止指针和整数之间的比较”。有谁知道如何解决这个问题?或者更好的是,采取不同的方式来解决这个问题?我觉得我的上述功能非常暴力。

How can I convert a character into an integer value? For example, I've been trying to read "A" as 1, "B" as 2, and so forth. I tried comparing the character to each letter in the alphabet and return the appropriate value.

int intvalue(char letter)
{
if(letter == "A")
    return 1;
else if(letter == "B")
    return 2;
else if(letter == "C")
    return 3;
else if(letter == "D")
    return 4;
else if(letter == "E")
    return 5;
else if(letter == "F")
    return 6;
else if(letter == "G")
    return 7;
else if(letter == "H")
    return 8;
else if(letter == "I")
    return 9;
else if(letter == "J")
    return 10;
else if(letter == "K")
    return 11;
else if(letter == "L")
    return 12;
else if(letter == "M")
    return 13;
else if(letter == "N")
    return 14;
else if(letter == "O")
    return 15;
else if(letter == "P")
    return 16;
else if(letter == "Q")
    return 17;
else if(letter == "R")
    return 18;
else if(letter == "S")
    return 19;
else if(letter == "T")
    return 20;
else if(letter == "U")
    return 21;
else if(letter == "V")
    return 22;
else if(letter == "W")
    return 23;
else if(letter == "X")
    return 24;
else if(letter == "Y")
    return 25;
else if(letter == "Z")
    return 26;

}

I got "error: ISO C++ forbids comparison between pointer and integer". Does anyone know how to fix this? Or even better, a different way to go about this? I feel like my above function is very brute-forceish.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

聽兲甴掵 2024-09-13 16:29:54

更可移植的实现涉及搜索字符数组并使用索引作为值:

int Value_From_Char(char c)
{
  static const char valid_letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  const std::string letter_str(valid_letters);

  // Search the letters for the given char.
  std::string::size_type  position = 0;
  position = letter_str.find(c);
  int result = 0;
  if (position == std::string::npos)
  {
    result = -1;
  }
  else
  {
    result = (int) position;
  }
  return result;
}

此函数也适用于 UTF 格式。此方法不对整理顺序做出任何假设。

A more portable implementation involves searching an array of characters and using the index as your value:

int Value_From_Char(char c)
{
  static const char valid_letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  const std::string letter_str(valid_letters);

  // Search the letters for the given char.
  std::string::size_type  position = 0;
  position = letter_str.find(c);
  int result = 0;
  if (position == std::string::npos)
  {
    result = -1;
  }
  else
  {
    result = (int) position;
  }
  return result;
}

This function will work with UTF formats as well. This method makes no assumptions about the collating sequence.

甜是你 2024-09-13 16:29:54

一种可能的方法是使用从 charint 的显式转换:

#include <iostream>

int main()
{
    char c;
    std::cout << "Enter a character: ";
    std::cin >> c; // for more than one chars you may need to use getline() or ignore()

    std::cout << "ASCII value of: " << c << ", is: " << int(c) <<".\n";
}

输入:

p

输出:

p 的 ASCII 值为:112。

A possible approach is just using an explicit cast from char to int:

#include <iostream>

int main()
{
    char c;
    std::cout << "Enter a character: ";
    std::cin >> c; // for more than one chars you may need to use getline() or ignore()

    std::cout << "ASCII value of: " << c << ", is: " << int(c) <<".\n";
}

Input:

p

Output:

ASCII value of: p, is: 112.

瀞厅☆埖开 2024-09-13 16:29:53

您需要使用字符文字,而不是字符串文字,例如,

if (letter == 'A')
              ^ note the single quotes

也就是说,如果您愿意假设您正在使用 ASCII 字符集的系统上运行,则可以简单地使用算术:

int charIndex = (letter - 'A') + 1;

在 ASCII 字符集中,字母是连续的索引,所以这有效。这可能不适用于其他字符集。

You need to use character literals, not string literals, e.g.,

if (letter == 'A')
              ^ note the single quotes

That said, if you are willing to assume you are running on a system using the ASCII character set, you can simply use arithmetic:

int charIndex = (letter - 'A') + 1;

In the ASCII character set, letters are at consecutive indices, so this works. This may not work with other character sets.

浮生面具三千个 2024-09-13 16:29:53

在 C++ 中,char 一个数字。因此不需要像上面那样复杂的方法:您可以直接使用 char 。如果你想让它基于 A=1 如上所示,那么你可以这样做:

int intValue(char value)
{
    return (value - 'A') + 1;
}

至于你得到的错误,在 C/C++ 中,双引号字符串是 char* (或者,更准确地说,是 char[],但这里的区别并不重要)。这就是您收到错误的原因:您正在尝试比较 charchar*

In C++, a char is a number. So there's no need for an elaborate method like above: you can just use the char directly. If you want to make it based with A=1 as shown above, then you can do:

int intValue(char value)
{
    return (value - 'A') + 1;
}

As for the error you're getting, in C/C++, a double-quoted string is a char* (or, more accurately, a char[], but the difference isn't important here). That's why you're getting an error: you're trying to compare between a char and a char*.

尛丟丟 2024-09-13 16:29:53

试试这个:(

return letter - 'A' + 1;

尽管您可能想要处理超出范围的letter输入)

请注意,char是单个字符,它使用单引号:' A'。字符串是多个字符并使用双引号:"ABC"

您可以将字符视为整数(它们的 ascii 值)并对其进行操作。

Try this:

return letter - 'A' + 1;

(Although you may want to handle an out-of-range letter input)

Note that char is a single character, which uses single quotes: 'A'. Strings are multiple characters and use double quotes: "ABC".

You can treat characters as integers (their ascii values) and manipulate them.

终遇你 2024-09-13 16:29:53

字符及其 ASCII 值整数是可以互换的。字符的 ASCII 值减去 'A' 的 ASCII 值将为 0,因此加 1 会得到 1:

return letter - 'A' + 1;

从技术上讲,您也可以只减去 '@' (前面的字符)表中的“A”),但我认为这可能不太清楚。您的问题是您使用的是 "A" 而不是 'A';前者是一个字符串(技术上是指向字符的指针),而后者是单个字符

Characters and their ASCII value integers are interchangeable. The character's ASCII value minus 'A's ASCII value would be 0, so adding 1 would give you 1:

return letter - 'A' + 1;

Technically you could also just subtract '@' (the character before 'A' in the table), but I think it's probably less clear. Your problem is you were using "A" instead of 'A'; the former is a string (technically a pointer to a character), while the latter is a single character

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文