SoFunction
Updated on 2025-05-13

Detailed explanation of common binary conversions in C language (from binary to hexadecimal)

1. Basics of the system

In mathematics and computer science,Pricing(also known as the number system) is a system used to represent numbers. Common binary systems are:

  • Binary: The cardinality is 2, and the commonly used symbol is0and1, widely used in computers.
  • Octal: The cardinality is 8, and the commonly used symbol is0-7, is more commonly used in early computer programming.
  • Decimal: The cardinality is 10, and the commonly used symbol is0-9, the binary system used in our daily lives.
  • hexadecimal: The cardinality is 16, and the commonly used symbol is0-9andA-F, widely used to represent memory addresses and other low-level operations in computers.

We will explore how to convert between these digits in the following content.

2. C language conversion

2.1 Convert from decimal to other binary

In C language, it is often necessary to convert decimal numbers into representations in other binary systems. It can be easily implemented using the formatted output function.

Decimal to binary

The C standard library does not directly provide functions that convert decimal to binary, but we can achieve this function through loops and bit operations.

#include <>

void decimalToBinary(int n) {
    if (n > 1) {
        decimalToBinary(n / 2);  // Recursively process high bits    }
    printf("%d", n % 2);  // Output the current bit binary}

int main() {
    int num = 29;
    printf("Decimal %d in Binary: ", num);
    decimalToBinary(num);
    printf("\n");
    return 0;
}

 Output

Decimal 29 in Binary: 11101
  • AnalysisdecimalToBinaryThe function continuously divides the decimal number by 2 through recursion until the quotient is 0, and then outputs the result from the lowest bit to the highest bit.

Decimal to octal

In C language, use%oThe format controller can directly output an octal representation.

#include <>

int main() {
    int num = 29;
    printf("Decimal %d in Octal: %o\n", num, num);  // %o output octal    return 0;
}

Output

Decimal 29 in Octal: 35
  • Analysis:here,%oThe octal representation used to print numbers.

Decimal to hexadecimal

Similarly, C language provides%Xor%xControl characters to output hexadecimal numbers.

#include <>

int main() {
    int num = 29;
    printf("Decimal %d in Hexadecimal: %X\n", num, num);  // %X output capital hexadecimal    return 0;
}

Output

Decimal 29 in Hexadecimal: 1D
  • Analysis%XPrint hexadecimal numbers and use capital letters (A-F)。

2.2 Convert from other binary to decimal

The C language standard library provides convenient functions to convert numbers in string form into decimal integers, such as strtol or strtoul. This makes it very easy to convert from other binary (such as binary, octal, hexadecimal) to decimal.

Binary to decimal

Since there is no direct standard function to convert binary strings to decimal, we can use the strtol function to convert binary strings to decimal.

#include <>
#include <>

int main() {
    const char* binary = "11101";  // Binary string    int num = strtol(binary, NULL, 2);  // 2 means binary    printf("Binary %s in Decimal: %d\n", binary, num);
    return 0;
}

Output

Binary 11101 in Decimal: 29
  • AnalysisstrtolThe given binary string"11101"Convert to decimal integer29, parameters2The string indicating that the input is binary.

Octal to decimal

Similarly, we can usestrtolConvert an octal string to decimal:

#include <>
#include <>

int main() {
    const char* octal = "35";  // Octal string    int num = strtol(octal, NULL, 8);  // 8 means octal    printf("Octal %s in Decimal: %d\n", octal, num);
    return 0;
}

Output

Octal 35 in Decimal: 29

Hexadecimal to decimal

Similarly, hexadecimal strings can also be passedstrtolConvert the function to decimal:

#include <>
#include <>

int main() {
    const char* hex = "1D";  // Hexadecimal string    int num = strtol(hex, NULL, 16);  // 16 means hexadecimal    printf("Hexadecimal %s in Decimal: %d\n", hex, num);
    return 0;
}

Output

Hexadecimal 1D in Decimal: 29
  • Analysis:here,strtolReplace hexadecimal string"1D"Convert to decimal integer29, parameters16The string indicating that the input is in hexadecimal.

3. Practical scenarios for phase conversion

Category conversion has practical uses in many applications, especially when it comes to underlying operations of computer systems. Here are some common application scenarios:

  • Memory address and pointer: When debugging programs, memory addresses are usually represented in hexadecimal, and the conversion of the binary helps understand the distribution of addresses.
  • File and data streams: In network programming and binary file processing, it is often necessary to convert between different digits, especially when processing byte streams in network protocols.
  • Hardware programming: In embedded systems and hardware programming, binary or hexadecimal is often required to represent the status of the device, control registers, etc.

4. Summary

In C language, phase conversion is a very practical skill. With built-in formatted output (such as %o, %x) and string conversion functions (such as strtol), we can easily convert between binary, octal, decimal and hexadecimal.

Today we learned:

  • How to convert decimal numbers to binary, octal and hexadecimal.
  • How to convert binary, octal, hexadecimal strings to decimal numbers.
  • Practical application of phase conversion in computer programming.

This is the article about the detailed explanation of the C-language conversion (from binary to hexadecimal) in C. For more related C-language conversion content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!