 # New Method to ASCII To int

Dear All,

I need to send two number to arduino using Bluetooth command line app. Then that two numbers assign to two variable and finally that two variables add together result to serial monitor

x = 3;

y = 4;

z = x+y;

Serial.println(z);

Subtract '0'. Note the single quotes. You're subtracting the ascii code for zero to get an integer.

Here is an example of converting ASCII strings to bytes in both decimal and hex:

``````#include <Arduino.h>
#include <stdlib.h>

void setup()
{
Serial.begin(9600);             //! Initialize the serial port to the PC

char dec_string[] = "123";
uint8_t dec_num;
char hex_string[] = "A5";
uint8_t hex_num;

dec_num = atoi(dec_string); // function from stdlib.h to convert ASCII strings into base10 numbers.
Serial.println(dec_string);
Serial.println(dec_num);

hex_num = strtol(hex_string, NULL, 16); // function to convert ASCII strings into base passed as arg.
Serial.println(hex_string);
Serial.println(hex_num, HEX);

}

void loop()
{
}
``````

The output will be this:

123
123
A5
A5

Note that the general function used for hex conversion takes ~4x as long as the decimal one.

Delta_G: Subtract '0'. Note the single quotes. You're subtracting the ascii code for zero to get an integer.

Thanks

Can I have the mathematics behind this method

PA3040: Thanks

Can I have the mathematics behind this method

Go look at an ascii table and you'll see it.

Delta_G: Go look at an ascii table and you'll see it.

As per the ascii table reduce 48 also he same

Am I write

Yes. 48 and '0' are exactly the same thing.

Yes. ASCII '0' is 0x30 (or 48 decimal). ASCII '1' is 0x31 (or 49 decimal. And so on. So subtracting ASCII '0' will give the integers from 0 to 9 for the ASCII '0' to '9'.

Thanks for all

Please advice to convert two digit numbers to Integer Like 10 11 12

PA3040: Thanks for all

Please advice to convert two digit numbers to Integer Like 10 11 12

I posted the code for that as #3. See above.

PA3040: Thanks for all

Please advice to convert two digit numbers to Integer Like 10 11 12

You have to go through one digit at a time.

Multiply first digit by 10 and add second digit.

Example: 32 = ( 3 * 10 ) + 2