Convert Binary to Hexadecimal number
Binary and hexadecimal are two different number systems used to represent numbers in computing. Binary is a base-2 number system that uses only two digits, 0 and 1, to represent numbers. Hexadecimal is a base-16 number system that uses 16 digits, from 0 to 9 and A to F, to represent numbers.
To convert a binary number to a hexadecimal number, we can group the binary digits into sets of four, starting from the rightmost digit. If the number of digits is not a multiple of four, we can add leading zeros to make it so. Then, we can convert each set of four binary digits to a single hexadecimal digit using the following table:
Binary | Hexadecimal |
---|---|
0000 | 0 |
0001 | 1 |
0010 | 2 |
0011 | 3 |
0100 | 4 |
0101 | 5 |
0110 | 6 |
0111 | 7 |
1000 | 8 |
1001 | 9 |
1010 | A |
1011 | B |
1100 | C |
1101 | D |
1110 | E |
1111 | F |
We can then write the hexadecimal digits in order, starting from the leftmost digit, to get the hexadecimal representation of the binary number.
For example, to convert the binary number 11011011 to hexadecimal, we can group the binary digits into sets of four: 1101 1011. Then we can convert each set of four binary digits to a single hexadecimal digit: D B. Therefore, the hexadecimal representation of the binary number 11011011 is DB.
Program Solution
//C Program
//Convert Binary to Hexadecimal number
#include <stdio.h>
//Reverse the given character array by given indexes
void reverse(char str[], int start, int end)
{
char temp;
for (start; start < end; start++, end--)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
}
}
//Convert decimal to hexadecimal
void decimal_hexadecimal(int number, char result[])
{
int digit = 0;
int index = 0;
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result[index] = digit + 48;
}
else if (digit > 10)
{
//55 ASCII of '7'
result[index] = digit + 55;
}
index++;
number /= 16;
}
if (index >= 1)
{
//add null char
result[index] = '\0';
reverse(result, 0, index - 1);
}
}
void binary_to_hexadecimal(long long number)
{
int result = 0, counter = 0;
int flag = 0;
if (number < 0)
{
flag = 1;
}
printf("Binary : %lld ", number);
//Convert binary to decimal
while (number != 0)
{
if ((number & 1) == 0b1)
{
result += (1 << (counter));
}
counter++;
number /= 10;
}
//Store hexa decimal values
//Assume that result is under 15 characters
char output[15];
//Convert decimal to hexa decimal
decimal_hexadecimal(result, output);
if (flag == 0)
{
printf(" Hexadecimal : %s \n", output);
}
else
{
printf(" Hexadecimal : -%s \n", output);
}
}
int main()
{
//Test Case
//Pass a decimal number which is contain binary values
binary_to_hexadecimal(1111);
binary_to_hexadecimal(10111);
binary_to_hexadecimal(101011);
binary_to_hexadecimal(11011);
binary_to_hexadecimal(-1000110);
return 0;
}
Output
Binary : 1111 Hexadecimal : F
Binary : 10111 Hexadecimal : 17
Binary : 101011 Hexadecimal : 2B
Binary : 11011 Hexadecimal : 1B
Binary : -1000110 Hexadecimal : -46
/*
C++ Program
Convert Binary to Hexadecimal number
*/
#include<iostream>
using namespace std;
class MyNumber
{
public:
//Convert decimal to hexadecimal
string decimal_hexadecimal(int number)
{
string result = "";
int digit = 0;
int index = 0;
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result = (char)(digit + 48) + result;
}
else if (digit > 10)
{
//55 ASCII of '7'
result = (char)(digit + 55) + result;
}
index++;
number /= 16;
}
return result;
}
void binary_to_hexadecimal(long number)
{
int result = 0, counter = 0;
cout << "Binary : " << number;
bool flag = false;
if (number < 0)
{
flag = true;
}
//Convert binary to decimal
while (number != 0)
{
if ((number & 1) == 0b1)
{
result += (1 << (counter));
}
counter++;
number /= 10;
}
//Convert decimal to hexa decimal
string output = this->decimal_hexadecimal(result);
if (flag == true)
{
//When negative element exist
output = "-" + output;
}
cout << " Hexadecimal : " << output << " \n";
}
};
int main()
{
MyNumber obj = MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_hexadecimal(1111);
obj.binary_to_hexadecimal(10111);
obj.binary_to_hexadecimal(101011);
obj.binary_to_hexadecimal(11011);
obj.binary_to_hexadecimal(-1000110);
return 0;
}
Output
Binary : 1111 Hexadecimal : F
Binary : 10111 Hexadecimal : 17
Binary : 101011 Hexadecimal : 2B
Binary : 11011 Hexadecimal : 1B
Binary : -1000110 Hexadecimal : -46
/*
Java Program
Convert Binary to Hexadecimal number
*/
public class MyNumber
{
//Convert decimal to hexadecimal
public String decimal_hexadecimal(int number)
{
String result="";
int digit = 0;
int index = 0;
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result =(char)(digit + 48) + result;
}
else if (digit > 10)
{
//55 ASCII of '7'
result =(char)(digit + 55) + result;
}
index++;
number /= 16;
}
return result;
}
public void binary_to_hexadecimal(long number)
{
int result = 0, counter = 0;
System.out.print("Binary : "+ number);
boolean flag = false;
if(number<0)
{
flag=true;
}
//Convert binary to decimal
while (number != 0)
{
if ((number & 1) == 0b1)
{
result += (1 << (counter));
}
counter++;
number /= 10;
}
//Convert decimal to hexa decimal
String output=decimal_hexadecimal(result);
if(flag==true)
{
//When negative element exist
output="-"+output;
}
System.out.print(" Hexadecimal : "+output+" \n" );
}
public static void main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_hexadecimal(1111);
obj.binary_to_hexadecimal(10111);
obj.binary_to_hexadecimal(101011);
obj.binary_to_hexadecimal(11011);
obj.binary_to_hexadecimal(-1000110);
}
}
Output
Binary : 1111 Hexadecimal : F
Binary : 10111 Hexadecimal : 17
Binary : 101011 Hexadecimal : 2B
Binary : 11011 Hexadecimal : 1B
Binary : -1000110 Hexadecimal : -46
/*
C# Program
Convert Binary to Hexadecimal number
*/
using System;
public class MyNumber
{
//Convert decimal to hexadecimal
public String decimal_hexadecimal(int number)
{
String result = "";
int digit = 0;
int index = 0;
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result = (char)(digit + 48) + result;
}
else if (digit > 10)
{
//55 ASCII of '7'
result = (char)(digit + 55) + result;
}
index++;
number /= 16;
}
return result;
}
public void binary_to_hexadecimal(long number)
{
int result = 0, counter = 0;
Console.Write("Binary : " + number);
Boolean flag = false;
if (number < 0)
{
flag = true;
}
//Convert binary to decimal
while (number != 0)
{
if ((number & 1) == 0b1)
{
result += (1 << (counter));
}
counter++;
number /= 10;
}
//Convert decimal to hexa decimal
String output = decimal_hexadecimal(result);
if (flag == true)
{
//When negative element exist
output = "-" + output;
}
Console.Write(" Hexadecimal : " + output + " \n");
}
public static void Main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_hexadecimal(1111);
obj.binary_to_hexadecimal(10111);
obj.binary_to_hexadecimal(101011);
obj.binary_to_hexadecimal(11011);
obj.binary_to_hexadecimal(-1000110);
}
}
Output
Binary : 1111 Hexadecimal : F
Binary : 10111 Hexadecimal : 17
Binary : 101011 Hexadecimal : 2B
Binary : 11011 Hexadecimal : 1B
Binary : -1000110 Hexadecimal : -46
<?php
/*
Php Program
Convert Binary to Hexadecimal number
*/
class MyNumber
{
//Convert decimal to hexadecimal
public function decimal_hexadecimal($number)
{
$result = "";
$digit = 0;
$index = 0;
while ($number != 0)
{
$digit = $number % 16;
if ($digit < 0)
{
$digit = -$digit;
}
if ($digit < 10)
{
//48 ASCII of '0'
$result = chr(($digit + 48)) . $result;
}
else if ($digit > 10)
{
//55 ASCII of '7'
$result = chr(($digit + 55)) . $result;
}
$index++;
$number = intval($number / 16);
}
return $result;
}
public function binary_to_hexadecimal($number)
{
$result = 0;
$counter = 0;
echo("Binary : ". $number);
$flag = false;
if ($number < 0)
{
$flag = true;
}
//Convert binary to decimal
while ($number != 0)
{
if (($number & 1) == 0b1)
{
$result += (1 << ($counter));
}
$counter++;
$number = intval($number / 10);
}
//Convert decimal to hexa decimal
$output = $this->decimal_hexadecimal($result);
if ($flag == true)
{
//When negative element exist
$output = "-". $output;
}
echo(" Hexadecimal : ". $output ." \n");
}
}
function main()
{
$obj = new MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
$obj->binary_to_hexadecimal(1111);
$obj->binary_to_hexadecimal(10111);
$obj->binary_to_hexadecimal(101011);
$obj->binary_to_hexadecimal(11011);
$obj->binary_to_hexadecimal(-1000110);
}
main();
Output
Binary : 1111 Hexadecimal : F
Binary : 10111 Hexadecimal : 17
Binary : 101011 Hexadecimal : 2B
Binary : 11011 Hexadecimal : 1B
Binary : -1000110 Hexadecimal : -46
/*
Node Js Program
Convert Binary to Hexadecimal number
*/
class MyNumber
{
//Convert decimal to hexadecimal
decimal_hexadecimal(number)
{
var result = "";
var digit = 0;
var index = 0;
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result = String.fromCharCode((digit + 48)) + result;
}
else
if (digit > 10)
{
//55 ASCII of '7'
result = String.fromCharCode((digit + 55)) + result;
}
index++;
number = parseInt(number / 16);
}
return result;
}
binary_to_hexadecimal(number)
{
var result = 0;
var counter = 0;
process.stdout.write("Binary : " + number);
var flag = false;
if (number < 0)
{
flag = true;
}
//Convert binary to decimal
while (number != 0)
{
if ((number & 1) == 0b1)
{
result += (1 << (counter));
}
counter++;
number = parseInt(number / 10);
}
//Convert decimal to hexa decimal
var output = this.decimal_hexadecimal(result);
if (flag == true)
{
//When negative element exist
output = "-" + output;
}
process.stdout.write(" Hexadecimal : " + output + " \n");
}
}
function main(args)
{
var obj = new MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_hexadecimal(1111);
obj.binary_to_hexadecimal(10111);
obj.binary_to_hexadecimal(101011);
obj.binary_to_hexadecimal(11011);
obj.binary_to_hexadecimal(-1000110);
}
main();
Output
Binary : 1111 Hexadecimal : F
Binary : 10111 Hexadecimal : 17
Binary : 101011 Hexadecimal : 2B
Binary : 11011 Hexadecimal : 1B
Binary : -1000110 Hexadecimal : -46
# Python 3 Program
# Convert Binary to Hexadecimal number
class MyNumber :
# Convert decimal to hexadecimal
def decimal_hexadecimal(self, number) :
result = ""
digit = 0
index = 0
while (number != 0) :
digit = number % 16
if (digit < 0) :
digit = -digit
if (digit < 10) :
# 48 ASCII of '0'
result = chr((digit + 48)) + result
elif (digit > 10) :
# 55 ASCII of '7'
result = chr((digit + 55)) + result
index += 1
number = int(number / 16)
return result
def binary_to_hexadecimal(self, number) :
result = 0
counter = 0
print("Binary : ", number,end="")
flag = False
if (number < 0) :
flag = True
# Convert binary to decimal
while (number != 0) :
if ((number & 1) == 0b1) :
result += (1 << (counter))
counter += 1
number = int(number / 10)
# Convert decimal to hexa decimal
output = self.decimal_hexadecimal(result)
if (flag == True) :
# When negative element exist
output = "-"+ output
print(" Hexadecimal : ", output ," \n")
def main() :
obj = MyNumber()
# Test Cases
# Pass a decimal number which is contain binary values
obj.binary_to_hexadecimal(1111)
obj.binary_to_hexadecimal(10111)
obj.binary_to_hexadecimal(101011)
obj.binary_to_hexadecimal(11011)
obj.binary_to_hexadecimal(-1000110)
if __name__ == "__main__": main()
Output
Binary : 1111 Hexadecimal : F
Binary : 10111 Hexadecimal : 17
Binary : 101011 Hexadecimal : 2B
Binary : 11011 Hexadecimal : 1B
Binary : -1000110 Hexadecimal : -46
# Ruby Program
# Convert Binary to Hexadecimal number
class MyNumber
# Convert decimal to hexadecimal
def decimal_hexadecimal(number)
result = ""
digit = 0
index = 0
while (number != 0)
digit = number % 16
if (digit < 0)
digit = -digit
end
if (digit < 10)
# 48 ASCII of '0'
result = ((digit + 48)).chr + result
elsif (digit > 10)
# 55 ASCII of '7'
result = ((digit + 55)).chr + result
end
index += 1
number /= 16
end
return result
end
def binary_to_hexadecimal(number)
result = 0
counter = 0
print("Binary :", number)
flag = false
if (number < 0)
number=-number
flag = true
end
# Convert binary to decimal
while (number != 0)
if ((number & 1) == 0b1)
result += (1 << (counter))
end
counter += 1
number /= 10
end
# Convert decimal to hexa decimal
output = self.decimal_hexadecimal(result)
if (flag == true)
# When negative element exist
output = "-"+ output
end
print(" Hexadecimal :", output ," \n")
end
end
def main()
obj = MyNumber.new()
# Test Cases
# Pass a decimal number which is contain binary values
obj.binary_to_hexadecimal(1111)
obj.binary_to_hexadecimal(10111)
obj.binary_to_hexadecimal(101011)
obj.binary_to_hexadecimal(11011)
obj.binary_to_hexadecimal(-1000110)
end
main()
Output
Binary :1111 Hexadecimal :F
Binary :10111 Hexadecimal :17
Binary :101011 Hexadecimal :2B
Binary :11011 Hexadecimal :1B
Binary :-1000110 Hexadecimal :-46
/*
Scala Program
Convert Binary to Hexadecimal number
*/
class MyNumber
{
//Convert decimal to hexadecimal
def decimal_hexadecimal(num: Int): String = {
var result: String = "";
var digit: Int = 0;
var index: Int = 0;
var number : Int = num;
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result = ""+(digit + 48).toChar + (result);
}
else if (digit > 10)
{
//55 ASCII of '7'
result = ""+(digit + 55).toChar + result;
}
index += 1;
number = (number / 16).toInt;
}
return result;
}
def binary_to_hexadecimal(num: Int): Unit = {
var result: Int = 0;
var counter: Int = 0;
var number : Int = num;
print("Binary : " + number);
var flag: Boolean = false;
if (number < 0)
{
flag = true;
}
//Convert binary to decimal
while (number != 0)
{
if ((number & 1) == 1)
{
result += (1 << (counter));
}
counter += 1;
number = (number / 10).toInt;
}
//Convert decimal to hexa decimal
var output: String = decimal_hexadecimal(result);
if (flag == true)
{
//When negative element exist
output = "-" + output;
}
print(" Hexadecimal : " + output + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_hexadecimal(1111);
obj.binary_to_hexadecimal(10111);
obj.binary_to_hexadecimal(101011);
obj.binary_to_hexadecimal(11011);
obj.binary_to_hexadecimal(-1000110);
}
}
Output
Binary : 1111 Hexadecimal : F
Binary : 10111 Hexadecimal : 17
Binary : 101011 Hexadecimal : 2B
Binary : 11011 Hexadecimal : 1B
Binary : -1000110 Hexadecimal : -46
/*
Swift Program
Convert Binary to Hexadecimal number
*/
class MyNumber
{
//Convert decimal to hexadecimal
func decimal_hexadecimal(_ num: Int) -> String
{
var result: String = "";
var digit: Int = 0;
var index: Int = 0;
var number: Int = num;
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of "0"
result = String(UnicodeScalar(UInt8(digit + 48))) + result;
}
else
if (digit > 10)
{
//55 ASCII of "7"
result = String(UnicodeScalar(UInt8(digit + 55))) + result;
}
index += 1;
number /= 16;
}
return result;
}
func binary_to_hexadecimal(_ num: Int)
{
var result: Int = 0;
var counter: Int = 0;
var number: Int = num
print("Binary : ", number,terminator:"");
var flag: Bool = false;
if (number < 0)
{
flag = true;
}
//Convert binary to decimal
while (number != 0)
{
if ((number & 1) == 0b1)
{
result += (1 << (counter));
}
counter += 1;
number /= 10;
}
//Convert decimal to hexa decimal
var output: String = self.decimal_hexadecimal(result);
if (flag == true)
{
//When negative element exist
output = "-" + output;
}
print(" Hexadecimal : ", output ," \n");
}
}
func main()
{
let obj: MyNumber = MyNumber();
//Test Cases
//Pass a decimal number which is contain binary values
obj.binary_to_hexadecimal(1111);
obj.binary_to_hexadecimal(10111);
obj.binary_to_hexadecimal(101011);
obj.binary_to_hexadecimal(11011);
obj.binary_to_hexadecimal(-1000110);
}
main();
Output
Binary : 1111 Hexadecimal : F
Binary : 10111 Hexadecimal : 17
Binary : 101011 Hexadecimal : 2B
Binary : 11011 Hexadecimal : 1B
Binary : -1000110 Hexadecimal : -46
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment