# Convert Characters (A,B C) to Number (1,2,3) Characters are sent from Processing

Hi, im sending one digit and one character from processing. E.g processing sends 1A or 1B, never more, never less than one digit + one character. I need to be able to send A-N from processing.
Arduino store the digit in 'char typeID; ' and the character in 'int motorID;'
This works but i need to convert the characters (in the motorID var) to numbers where A = 1, B=2… Which would be the easiest way of doing this?

This is how I receive the vars:

`````` typeID = Serial.read();                 // read it and store it in typeID
}
``````
``````motorID  - 'A' + 1
``````

I'm trying to follow along for learning purposes so I looked up the ASCII values for 'A' and '1' and the letters are = to '[num]'+0x30.
(ie: 1=0x31, A=0x61) so couldn't you just do 'A'-0x30 to convert 'A' to '1' ?

To convert 'A' to '1' instead of 1 you would do this...

motorID - 'A' + '1'

Assuming you got 'A' from the serial port then

``````x = Serial.read();
x = (x - 'A') + 1;  // () not really required
``````

will give you an ID of 1 if that's the required relationship and it seems that it is. This translates to

65 - 65 + 1

Rob

Ok, I see where I got confused, that's DEC and I was trying to do with HEX.

The thing to note is that the upper case letters are in order as are the lower case and the numbers.

So 'A' -'A' givers zero. Even better is that '0'-0' gives zero.

Mark

Hmm, thanks for all replies but Im a bit confused.

I send A from processing, then I store it in typeID. When Serial.Print(motorID); it outputs '17'.

Can't find out why A translates to 17.

EDIT*

Now I managed to make it so that sending an 'A' from processing prints an 'A'.

So when i convert it ill do
motorID=motorID - 'A' + '1';

since ill be sending A-N, how can i convert it in a smart way? Sometimes 'A' will be sent 4 times, 'N' nine times, so on… And i need N to = 14.

EDIT*

Looks like motorID=motorID - 'A' + '1'; did all the tricks! Ill do some more testing now.