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