Hexadecimal
Hexadecimal is a base-16 number system that uses sixteen digits: 0-9 and A-F. It is commonly used in computing to represent binary-coded values more concisely. Each hexadecimal digit represents four bits, allowing for a more compact representation of binary data.
In the hexadecimal system, the digits 0-9 represent values 0-9, and the letters A-F represent values 10-15. Hexadecimal is often used in programming for memory addresses, color codes, and other low-level data representations.
Conversion from Decimal
- Divide the decimal number by 16 and note down the remainder.
- Repeat Step 1 with the quotient until the quotient becomes 0.
- Convert each remainder to its hexadecimal equivalent.
For example, let's convert the decimal number 345 to hexadecimal:
Hexadecimal equivalents:
, ,
Therefore,
While most programming languages provide built-in functions for converting between decimal and hexadecimal, here's how you can implement it yourself:
def decimal_to_hexadecimal(decimal: int) -> str:
hexadecimal = ""
while decimal > 0:
remainder = decimal % 16
if remainder < 10:
hexadecimal = str(remainder) + hexadecimal
else:
hexadecimal = chr(ord('A') + remainder - 10) + hexadecimal
decimal //= 16
return hexadecimal
print(decimal_to_hexadecimal(30)) # 1E
function decimalToHexadecimal(decimal) {
let hexadecimal = "";
while (decimal > 0) {
const remainder = decimal % 16;
if (remainder < 10) {
hexadecimal = remainder + hexadecimal;
} else {
hexadecimal = String.fromCharCode('A'.charCodeAt(0) + remainder - 10) + hexadecimal;
}
decimal = Math.floor(decimal / 16);
}
return hexadecimal;
}
console.log(decimalToHexadecimal(30)); // 1E
#include <iostream>
#include <sstream>
using namespace std;
string decimalToHexadecimal(int decimal) {
stringstream ss;
while (decimal > 0) {
int remainder = decimal % 16;
if (remainder < 10) {
ss << remainder;
} else {
ss << char('A' + remainder - 10);
}
decimal /= 16;
}
string hexadecimal = ss.str();
reverse(hexadecimal.begin(), hexadecimal.end());
return hexadecimal.empty() ? "0" : hexadecimal;
}
int main() {
cout << decimalToHexadecimal(30) << endl; // 1E
}
fn decimal_to_hexadecimal(decimal: i32) -> String {
let mut hexadecimal = String::new();
let mut decimal = decimal;
while decimal > 0 {
let remainder = decimal % 16;
if remainder < 10 {
hexadecimal = format!("{}{}", remainder, hexadecimal);
} else {
hexadecimal = format!("{}{}", (b'A' + remainder as u8 - 10) as char, hexadecimal);
}
decimal /= 16;
}
hexadecimal
}
fn main() {
println!("{}", decimal_to_hexadecimal(30)); // 1E
}
You can use the built-in functions to convert between decimal and hexadecimal:
print(hex(30)) # 0x1e
console.log((30).toString(16)); // 1e
#include <iostream>
using namespace std;
int main() {
cout << hex << 30 << endl; // 1e
}
fn main() {
println!("{:x}", 30); // 1e
}
Conversion from Binary
Converting binary to hexadecimal involves grouping binary digits into sets of four, and then converting each group to its hexadecimal equivalent. Here are the steps expressed as equations:
Let's assume we have a binary number
- First, group the binary digits into sets of four starting from the rightmost side:
- To find the number of groups, use the formula:
Then, pad the leftmost group with zeros if necessary so that it contains four digits.
For each group of four binary digits, convert them to their equivalent hexadecimal value using the formula:
- Finally, concatenate the hexadecimal values obtained from each group to get the hexadecimal representation of the binary number.
Let's break it down further with an example: Suppose we have the binary number
- Group the binary digits into sets of four:
Since there are 14 binary digits, we have
groups. Pad the leftmost group with zeros:
- Convert each group to its hexadecimal equivalent:
You may refer to the next section for the conversion table.
- Concatenate the hexadecimal values:
So, the binary number
Here's how you would convert binary to hexadecimal in various programming languages:
def binary_to_hexadecimal(binary: str) -> str:
return hex(int(binary, 2))[2:]
print(binary_to_hexadecimal("10111011001010")) # 2eca
function binaryToHexadecimal(binary) {
return parseInt(binary, 2).toString(16);
}
console.log(binaryToHexadecimal("10111011001010")); // 2eca
#include <iostream>
#include <bitset>
using namespace std;
int main() {
cout << hex << bitset<16>(stoi("10111011001010", 0, 2)).to_ulong() << endl; // 2eca
}
fn main() {
let binary_number = "10111011001010"; // Change this value to the binary number you want to convert
let decimal_number = i32::from_str_radix(binary_number, 2).unwrap(); // Convert binary to decimal
let hexadecimal_number = format!("{:X}", decimal_number); // Convert decimal to hexadecimal
println!("Binary {} is equivalent to Hexadecimal {}", binary_number, hexadecimal_number);
}
Conversion from Octal
Converting from octal to hexadecimal involves first converting the octal number to binary and then converting the binary number to hexadecimal:
- Convert the octal number to binary.
- Convert the binary number to hexadecimal.
Quite simple, isn't it?
Here's an example:
Let's convert the octal number
- First, convert the octal number to binary:
- Then, convert the binary number to hexadecimal:
So,
You can also implement the conversion in various languages (although not necessarily with the aforementioned steps):
octal = "352"
binary = bin(int(octal, 8))[2:]
hexadecimal = hex(int(binary, 2))[2:]
print(hexadecimal) # ea
const octal = "352";
const binary = parseInt(octal, 8).toString(2);
const hexadecimal = parseInt(binary, 2).toString(16);
console.log(hexadecimal); // ea
#include <iostream>
#include <sstream>
#include <iomanip>
int main() {
// Input octal number
std::string octalStr = "352";
int decimal;
std::istringstream(octalStr) >> std::oct >> decimal;
// Convert decimal integer to hexadecimal string
std::ostringstream hexStream;
hexStream << std::hex << decimal;
std::string hexStr = hexStream.str();
// Output hexadecimal equivalent
std::cout << "Hexadecimal equivalent: " << hexStr << std::endl;
return 0;
}
fn main() {
let octal_number = "377"; // Change this value to the octal number you want to convert
let decimal_number = i32::from_str_radix(octal_number, 8).unwrap(); // Convert octal to decimal
let hexadecimal_number = format!("{:X}", decimal_number); // Convert decimal to hexadecimal
println!("Octal {} is equivalent to Hexadecimal {}", octal_number, hexadecimal_number);
}