Newbie programmer here, and I needed to convert a character to an integer. After some googling, there are 2 main ways to do it (without making it a string first): subtracting by '0' and using atoi(). I understand how to use them and when, but I still don't have the faintist idea as to why they work the way they do. Subtracting by '0' destroys what little understanding I had of concatination, and the name choice of "atoi" baffles me. At first I thought it was shorthand of saying "__ to integer", but what does the 'a' mean??? And how is atoi any different from the mind-breaking char - '0'
'? So many questions...
So if I passed something like a char array to it, could also work? Nice
Ahhh, that makes sense. Sometimes it's easy to forget that chars are just numbers on a lookup table. Thanks man! I didn't think anyone would replay mere seconds after I asked my question
1. If you enter 1234 in the InputBox of the Serial Monitor (Fig-1) with Newline option and click on the Send Button, then the following data bytes (the ASCII codes for 1, 2, 3, 4, and Newline, Fig-2) are transmitted one-after-another as 10-bit frame (Fig-3) towards the UNO:
0x31 0x32 0x33 0x34 0x0A //the space for clarity and 0x for base
2. The UNO receives all the bytes of Step-1 and saves them in the unseen Serial Buffer. (The byte m = Serial.readBytesUntil('\n', myData, size of myData - 1); code can be used to transfer the data bytes of the Serial Buffer except the Newline character into a char-type user array, char myData[10].)
3. How to reconstruct/retrive the orginal decimal number (1234) from the ASCII elements of the array of Step-2?
4. There are two ways to answer the question of Step-3.
(1) Use atoi() (ASCII to decimal Integer number) function which requires that the last element of the user array of Step-2 must have a null-character (0x00 = '\0').
myData[4] = '\0'; insert null-charcter
int y = atoi(myData);
Serial.print(y, DEC); shows: 1234
(2) Use of Manual Appproach
(a) Extractt 1 from 0x31 which is the item-0 of myData[] array.
byte y0 = myData[0] - '0'
==> byte y0 = 0x31 - 0x30 //see Fig-2
==> byte y0 = 0x01
(b) Similarly, extract y1 = 0x02, y2 = 0x03, and y3 = 0x04 from the remainging items of the array.
(c) Now, perform SHFIT and OR/AND operatons on y0, y1, y2, and y3 to get: y = 1234.
//------------------------------------------------------------------------------------------------------------
Figure-1:
Figure-2:
Figure-3:
Remember that atoi() (ascii to integer) requires a cString as an input so you need a null termination in your char array.
Subtracting '0' to a char works not really because it’s ASCII encoding but more importantly because digits are coded from '0' to '9' successively/contiguously. If they had been arranged in another way in the table then it would not work.
Misleading our newbie. The Uno received the 0x0A. The method used in your code example to extract the characters from the buffer discarded the 0x0A. From Reference for Serial.readBytesUntil():
The terminator character is discarded from the serial buffer, unless the number of characters read and copied into the buffer equals
length
.
I am editing my post #6.
This is what I have thought to get the image 1234 on the Serial Monitor:
void setup()
{
Serial.begin(9600);
//---Given--------
byte y0 = 0x01; //00000001
byte y1 = 0x02; //00000010
byte y2 = 0x03; //00000011
byte y3 = 0x04; //00001000
//-----------------------
byte y0y1 = (y0 << 4) | y1; //yoy1 = 00010010
byte y2y3 = (y2 << 4) | y3; //y2y3 = 00100011
//-------------------------------------------
int y = (int)(y0y1 << 8)|(int)y2y3; //y = 00010010 00100011
Serial.print(y, HEX); //shows: 1234
}
void loop()
{
}
Output:
1234
In the Forum -- not everything always comes meaningful! It took centuries for the history of science to get rid of the mythologies that it contained in the name of science?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.