Program to Convert Hexadecimal to Decimal
- Write a program to Convert Hexadecimal to Decimal in C
- Write a program to Convert Hexadecimal to Decimal in C++
- Write a program to Convert Hexadecimal to Decimal in Python
- Write a program to Convert Hexadecimal to Decimal in PHP
- Write a program to Convert Hexadecimal to Decimal in Java
- Write a program to Convert Hexadecimal to Decimal in Java Script
- Write a program to Convert Hexadecimal to Decimal in C#
Explanation:
Understand the Hexadecimal System:
- The hexadecimal system is base-16, with digits 0-9 representing values 0-9 and letters A-F representing values 10-15.
Use Positional Weighting:
- Each digit in the hexadecimal number represents a power of 16 based on its position (rightmost position is 160, next is 161, and so on).
- Multiply each digit’s value by 16n, where n is the position index (starting from 0 for the rightmost digit).
Sum the Results:
- Add up all the weighted values to get the decimal equivalent.
Example Conversion:
Hexadecimal: 2F
- Expand using powers of 16:
- 2⋅161 = 2⋅16 = 32
- F⋅160 = 15⋅1 = 15
- Add the results:
- 32 + 15 = 47
Decimal Result: 47
Program to Convert Hexadecimal to Decimal
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
#include <string.h>
#include <math.h>
int hexToDecimal(char hex[]) {
int decimal = 0, base = 1;
for (int i = strlen(hex) - 1; i >= 0; i--) {
if (hex[i] >= '0' && hex[i] <= '9') {
decimal += (hex[i] - '0') * base;
} else if (hex[i] >= 'A' && hex[i] <= 'F') {
decimal += (hex[i] - 'A' + 10) * base;
} else if (hex[i] >= 'a' && hex[i] <= 'f') {
decimal += (hex[i] - 'a' + 10) * base;
}
base *= 16;
}
return decimal;
}
int main() {
char hex[100];
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
printf("Decimal: %d\n", hexToDecimal(hex));
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int hexToDecimal(string hex) {
int decimal = 0, base = 1;
for (int i = hex.length() - 1; i >= 0; i--) {
if (hex[i] >= '0' && hex[i] <= '9') {
decimal += (hex[i] - '0') * base;
} else if (hex[i] >= 'A' && hex[i] <= 'F') {
decimal += (hex[i] - 'A' + 10) * base;
} else if (hex[i] >= 'a' && hex[i] <= 'f') {
decimal += (hex[i] - 'a' + 10) * base;
}
base *= 16;
}
return decimal;
}
int main() {
string hex;
cout << "Enter a hexadecimal number: ";
cin >> hex;
cout << "Decimal: " << hexToDecimal(hex) << endl;
return 0;
}
def hex_to_decimal(hexadecimal):
return int(hexadecimal, 16)
hexadecimal = input("Enter a hexadecimal number: ")
print("Decimal:", hex_to_decimal(hexadecimal))
<?php
function hexToDecimal($hex) {
return hexdec($hex);
}
$hex = readline("Enter a hexadecimal number: ");
echo "Decimal: " . hexToDecimal($hex) . "\n";
?>
import java.util.Scanner;
public class HexadecimalToDecimal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a hexadecimal number: ");
String hex = sc.nextLine();
int decimal = Integer.parseInt(hex, 16);
System.out.println("Decimal: " + decimal);
}
}
function hexToDecimal(hex) {
return parseInt(hex, 16);
}
const hex = prompt("Enter a hexadecimal number: ");
console.log("Decimal:", hexToDecimal(hex));
using System;
class HexadecimalToDecimal {
static void Main() {
Console.Write("Enter a hexadecimal number: ");
string hex = Console.ReadLine();
int decimalValue = Convert.ToInt32(hex, 16);
Console.WriteLine("Decimal: " + decimalValue);
}
}