To recap, you can write integer literals in any of the relevant bases (binary, octal, decimal, hexadecimal). This has no impact on how it is actually seen by the microcontroller, because it only understands binary.
These four lines are exactly the same. The compiler converts them all to 0b00010011.
It is only when you request to print the value, that it will be converted to text.
This text is also represented by binary numbers. The link between the actual number and the character they represent is completely arbitrary. Most of the time, ASCII is used (nowadays, it's mostly UTF-8, but it is compatible with ASCII for the most important characters).
For example:
---
|
``` Serial.print(0x13, BIN); // Prints "10011"
|
|
Serial.print(0x13, OCT); // Prints "23"
|
|
Serial.print(0x13, DEC); // Prints "19"
|
Serial.print(0x13, HEX); // Prints "13" ```
|
The string "10011" that is printed on the first line is represented by the numbers [0x31 0x30 0x30 0x31 0x31 0x00] (I wrote them here in hexadecimal for convenience, but as mentioned above the base of the number doesn't matter to the Arduino). All you need to remember is that the digit/character '0' is represented by number 0x30. The 0x00 byte (NULL) at the end is used to know where the text ends.
Note how I use single quotation marks for a single character, double quotes for a string of characters, and no quotation marks for normal numbers. This is the same as you'd write in your code.
The text representations are only to help humans read or write the numbers. All calculations on the Arduino happen in binary. It's also much less memory-efficient to save numbers as strings of text, for example, the number 128 can be represented as a single byte (0b10000000), but if you were to convert it to decimal text, you'd need 4 bytes (three characters + NULL), and if you'd convert it to binary text, you'd need 9 times more (eight characters + NULL).
The important thing to take away here is that there are three different scenarios where you represent numbers:
- The text representation you use in your code. This can be binary, octal, decimal or hexadecimal.
- The compiler converts these characters in your code to the binary representation that's used by the Arduino.
- The Arduino can then convert these binary represented numbers back into a string of text, using the print functions.
Unless you have to parse text data (from a text file on an SD card, from a protocol that uses ASCII like the HTTP protocol, etc.), it's not a good idea to save numbers as text on the Arduino.
Pieter