Introduction to Different Bases
A number system is used to represent different numbers. It provides a mathematical notation for writing numbers in different combinations and bases. Number system provides arithmetic and algebraic structure of numbers that computers and microcontrollers can process.
Base 16 or Hexadecimal number system is widely used in microcontrollers and microprocessor boards. Other bases systems such as Binary (Base 2) or Octal (Base 8) systems are also widely used.
Here are some examples of numbers represented in different bases:
DEC | Hex | Oct | Bin |
0 | 0 | 000 | 0000 |
1 | 1 | 001 | 0001 |
2 | 2 | 002 | 0010 |
3 | 3 | 003 | 0011 |
4 | 4 | 004 | 0100 |
5 | 5 | 005 | 0101 |
6 | 6 | 006 | 0110 |
7 | 7 | 007 | 0111 |
8 | 8 | 010 | 1000 |
9 | 9 | 011 | 1001 |
10 | A | 012 | 1010 |
11 | B | 013 | 1011 |
12 | C | 014 | 1100 |
Arduino and other microcontrollers process information in Binary and Hexadecimal format. As an Arduino microcontroller Atmega328p is eight bits so using Binary or Hexadecimal information is easy for processing.
We have covered the basic introduction of the number system. Now we will write Arduino code to convert a number into different bases.
Converting a Number to Different Bases Using Arduino IDE
Arduino programming comes with a format that allows to convert any number into different bases such as HEX, Binary or OCT. Now upload the given code in the Arduino board to convert any of the given numbers into OCT, HEX or binary.
Code
Open Arduino IDE and select the board you are using after that click upload:
This code started by initializing baud rate so we can see output on serial monitor. Here we have taken a number 10 which is converted into different bases such as BIN, HEX and OCT.
Serial.begin(9600);
Serial.print("Number 10 in BIN: ");
Serial.println(10, BIN);
Serial.print("Number 10 in HEX: ");
Serial.println(10, HEX);
Serial.print("Number 10 in OCT: ");
Serial.println(10, OCT);
}
void loop() {}
Output
In the output window we can see the number 10 is converted to all three different bases.
We have converted a number into different bases using the same format any decimal number can be converted into different number bases.
Conclusion
Arduino programming is very versatile in nature; it allows frequent conversions from one number base to another. Here this article provides a sample code for conversion on number 10 into different bases. To convert any other number, see the given example code.