Program to Convert Binary to Hexadecimal
- Write a program to Convert Binary to Hexadecimal in C
- Write a program to Convert Binary to Hexadecimal in C++
- Write a program to Convert Binary to Hexadecimal in Python
- Write a program to Convert Binary to Hexadecimal in PHP
- Write a program to Convert Binary to Hexadecimal in Java
- Write a program to Convert Binary to Hexadecimal in Java Script
- Write a program to Convert Binary to Hexadecimal in C#
Explanation:
The binary digits are grouped into sets of four, and each group is then converted straight to its hexadecimal equivalent in order to convert a binary number (base 2) to a hexadecimal number (base 16). One hexadecimal digit is equivalent to four binary digits, so this works.
Steps for Binary to Hexadecimal Conversion:
- Start with the binary number.
- For example: 110101111.
- Group the binary digits into sets of 4, starting from the right.
- Add leading zeros if necessary to ensure all groups have 4 digits.
- Example: 110101111 → 000110101111 → 0001 ∣ 1010 ∣ 1111
- Convert each group of 4 binary digits into its hexadecimal equivalent.
- 0001 → 1
- 1010 → A
- 1111 → F
- Combine the hexadecimal digits.
- 1AF (this is the hexadecimal representation).
Program to Convert Binary to Hexadecimal
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> #include <math.h> int binaryToDecimal(long long n) { int decimal = 0, base = 1, remainder; while (n > 0) { remainder = n % 10; decimal += remainder * base; base *= 2; n /= 10; } return decimal; } void decimalToHexadecimal(int decimal) { char hex[100]; int i = 0; while (decimal != 0) { int temp = decimal % 16; if (temp < 10) hex[i] = temp + 48; else hex[i] = temp + 55; decimal /= 16; i++; } for (int j = i - 1; j >= 0; j--) printf("%c", hex[j]); } int main() { long long binary; printf("Enter a binary number: "); scanf("%lld", &binary); int decimal = binaryToDecimal(binary); printf("Hexadecimal: "); decimalToHexadecimal(decimal); return 0; }
#include <iostream> #include <cmath> using namespace std; int binaryToDecimal(long long n) { int decimal = 0, base = 1; while (n > 0) { int remainder = n % 10; decimal += remainder * base; base *= 2; n /= 10; } return decimal; } void decimalToHexadecimal(int decimal) { string hex = ""; char hexChars[] = "0123456789ABCDEF"; while (decimal > 0) { hex = hexChars[decimal % 16] + hex; decimal /= 16; } cout << hex; } int main() { long long binary; cout << "Enter a binary number: "; cin >> binary; int decimal = binaryToDecimal(binary); cout << "Hexadecimal: "; decimalToHexadecimal(decimal); return 0; }
def binary_to_hexadecimal(binary): decimal = int(binary, 2) return hex(decimal).replace("0x", "").upper() binary = input("Enter a binary number: ") print("Hexadecimal:", binary_to_hexadecimal(binary))
<?php function binaryToHexadecimal($binary) { $decimal = bindec($binary); return strtoupper(dechex($decimal)); } $binary = readline("Enter a binary number: "); echo "Hexadecimal: " . binaryToHexadecimal($binary) . "\n"; ?>
import java.util.Scanner; public class BinaryToHexadecimal { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a binary number: "); String binary = sc.nextLine(); int decimal = Integer.parseInt(binary, 2); String hexadecimal = Integer.toHexString(decimal).toUpperCase(); System.out.println("Hexadecimal: " + hexadecimal); } }
function binaryToHexadecimal(binary) { const decimal = parseInt(binary, 2); return decimal.toString(16).toUpperCase(); } const binary = prompt("Enter a binary number: "); console.log("Hexadecimal:", binaryToHexadecimal(binary));
using System; class BinaryToHexadecimal { static void Main() { Console.Write("Enter a binary number: "); string binary = Console.ReadLine(); int decimalValue = Convert.ToInt32(binary, 2); string hexadecimal = decimalValue.ToString("X"); Console.WriteLine("Hexadecimal: " + hexadecimal); } }