views:

90

answers:

5

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.

A: 

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.

Stephen
+6  A: 

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.

James McNellis
I think in this day and age if there is any assumption at all one can make about an environment, it is relying on ASCII. Every assumption we avoid making increases system complexity, so at some point we have to find the balance, we have to find a solid common starting point.
wilhelmtell
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

Michael Mrozek
`letter - 'A' + 1` != `letter - 'B'`. Addition and subtraction have the same precedence, after all.
Ben Voigt
@Ben I had a feeling that was wrong, I was just doing the math in my head. Fixed
Michael Mrozek
+3  A: 

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

JSBangs
A: 

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.

Thomas Matthews