Convert Hex to Decimal in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
#include <string.h>
#include <math.h>
int main() {
    char hex[20];
    int decimal = 0, base = 1, len, i;
    printf("Enter hexadecimal: ");
    scanf("%s", hex);
    len = strlen(hex);
    for(i = len - 1; i >= 0; i--) {
        if(hex[i] >= '0' && hex[i] <= '9')
            decimal += (hex[i] - 48) * base;
        else if(hex[i] >= 'A' && hex[i] <= 'F')
            decimal += (hex[i] - 55) * base;
        else if(hex[i] >= 'a' && hex[i] <= 'f')
            decimal += (hex[i] - 87) * base;
        base *= 16;
    }
    printf("Decimal: %d", decimal);
    return 0;
}

C Output

Input:
1A

Output:
Decimal: 26


C++ Program

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main() {
    string hex;
    int decimal = 0, base = 1;
    cout << "Enter hexadecimal: ";
    cin >> hex;
    for (int i = hex.size() - 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;
    }
    cout << "Decimal: " << decimal;
}

C++ Output

Input:
FF

Output:
Decimal: 255


JAVA Program

import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter hexadecimal: ");
        String hex = sc.next();
        int decimal = Integer.parseInt(hex, 16);
        System.out.println("Decimal: " + decimal);
    }
}

JAVA Output

Input:
7B

Output:
Decimal: 123


Python Program

hex_num = input("Enter hexadecimal: ")
decimal = int(hex_num, 16)
print("Decimal:", decimal)

Python Output

Input:
2F

Output:
Decimal: 47


In-Depth Explanation
Example
Take the hexadecimal number 1A. Hexadecimal is base 16, so its digits go from 0–9 and A–F, where A=10, B=11, C=12, D=13, E=14, and F=15. The units are represented by the rightmost digit (16^0), followed by 16^1, then 16^2, and so on. 1A then becomes (1 × 16^1) + (10 × 16^0) = 16 + 10 = 26. That is why 1A equals 26 in decimal.

Real-Life Analogy
Imagine hexadecimal as another currency. If decimal is "rupees", hexadecimal is similar to "rupees + special coins" where the coins extend beyond 9 till F. When you convert, you're only changing representation between two systems of counting, similar to changing dollars to rupees — same value, different form.

Why It Matters
Hexadecimal finds extensive use in computer science as it efficiently represents binary data. A hex digit perfectly correlates to four binary bits, hence it's easily readable for humans compared to endless strings of binary data. Programmers employ hex for memory locations, web page color coding, and low-level data representation.

Learning Insights
Learning hex-to-decimal conversion instructs you in number system basics, base conversion principles, and positional value reasoning. These ideas are crucial in algorithms, digital electronics, computer design, and programming language syntax comprehension.

Real-World Application in Interviews & Projects
Interviewers will use this to gauge your logic-construction abilities and number system awareness. In actual projects, you could encounter hexadecimal when decoding network packets, handling file formats, or manipulating graphics and color codes such as #FF5733 in CSS.

SEO-Optimized Closing Paragraph
It is essential that you know how to convert from hexadecimal to decimal if you are in programming, computer science, or electronics. This simple yet powerful concept helps in understanding memory addressing, binary data handling, and digital logic design. Whether you’re preparing for coding interviews, working on embedded systems, or decoding color codes in web development, mastering hex-to-decimal conversion will boost your skills and confidence in number system conversions. With this guide, you now have explicit code examples in C, C++, Java, and Python, allowing you to easily implement this knowledge in practical applications.