C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Conversion Between Binary, Octal, Hexadecimal, And Decimal

In the C programming language, you can convert between binary, octal, hexadecimal, and decimal using built-in functions or by implementing your own conversion functions. Here are some examples:

  • Converting from binary to decimal: You can use the strtol function to convert a binary string to a decimal integer. The second argument of strtol should be NULL, and the third argument should be 2 to indicate that the input string is in binary format. For example:
char binaryStr[] = "1101";
int decimalNum = strtol(binaryStr, NULL, 2);
printf("Binary %s is decimal %d\n", binaryStr, decimalNum);
  • Converting from decimal to binary: You can use the sprintf function to convert a decimal integer to a binary string. The format specifier %b should be used to indicate that the output should be in binary format. For example:
int decimalNum = 13;
char binaryStr[33];
sprintf(binaryStr, "%b", decimalNum);
printf("Decimal %d is binary %s\n", decimalNum, binaryStr);
  • Converting from octal to decimal: You can use the strtol function to convert an octal string to a decimal integer. The second argument of strtol should be NULL, and the third argument should be 8 to indicate that the input string is in octal format. For example:
char octalStr[] = "15";
int decimalNum = strtol(octalStr, NULL, 8);
printf("Octal %s is decimal %d\n", octalStr, decimalNum);
  • Converting from decimal to octal: You can use the sprintf function to convert a decimal integer to an octal string. The format specifier %o should be used to indicate that the output should be in octal format. For example:
int decimalNum = 21;
char octalStr[12];
sprintf(octalStr, "%o", decimalNum);
printf("Decimal %d is octal %s\n", decimalNum, octalStr);
  • Converting from hexadecimal to decimal: You can use the strtol function to convert a hexadecimal string to a decimal integer. The second argument of strtol should be NULL, and the third argument should be 16 to indicate that the input string is in hexadecimal format. For example:
char hexStr[] = "FF";
int decimalNum = strtol(hexStr, NULL, 16);
printf("Hexadecimal %s is decimal %d\n", hexStr, decimalNum);
  • Converting from decimal to hexadecimal: You can use the sprintf function to convert a decimal integer to a hexadecimal string. The format specifier %X should be used to indicate that the output should be in uppercase hexadecimal format. For example:
int decimalNum = 255;
char hexStr[9];
sprintf(hexStr, "%X", decimalNum);
printf("Decimal %d is hexadecimal %s\n", decimalNum, hexStr);

Note that these are just examples of how to perform conversion between different number systems in C. There are many other ways to implement conversion functions, depending on your needs and programming goals.

  1. Binary to decimal conversion in C:

    #include <stdio.h>
    #include <math.h>
    
    int binaryToDecimal(long long binaryNumber) {
        int decimalNumber = 0, i = 0, remainder;
        while (binaryNumber != 0) {
            remainder = binaryNumber % 10;
            binaryNumber /= 10;
            decimalNumber += remainder * pow(2, i);
            ++i;
        }
        return decimalNumber;
    }
    
    int main() {
        long long binaryNumber = 1010;
        printf("Decimal representation: %d\n", binaryToDecimal(binaryNumber));
    
        return 0;
    }
    
    • Converts a binary number to its decimal representation.
  2. C program for decimal to binary conversion:

    #include <stdio.h>
    
    void decimalToBinary(int n) {
        if (n > 1)
            decimalToBinary(n / 2);
        printf("%d", n % 2);
    }
    
    int main() {
        int decimalNumber = 10;
        printf("Binary representation: ");
        decimalToBinary(decimalNumber);
    
        return 0;
    }
    
    • Converts a decimal number to its binary representation.
  3. Octal to decimal conversion in C language:

    #include <stdio.h>
    
    int main() {
        int octalNumber = 075;
        printf("Decimal representation: %d\n", octalNumber);
    
        return 0;
    }
    
    • Converts an octal number to its decimal representation.
  4. Hexadecimal to decimal conversion in C:

    #include <stdio.h>
    
    int main() {
        int hexadecimalNumber = 0xA;
        printf("Decimal representation: %d\n", hexadecimalNumber);
    
        return 0;
    }
    
    • Converts a hexadecimal number to its decimal representation.
  5. C code for binary to octal conversion:

    #include <stdio.h>
    
    int binaryToOctal(long long binaryNumber) {
        int octalNumber = 0, decimalNumber = 0, i = 0;
    
        while (binaryNumber != 0) {
            decimalNumber += (binaryNumber % 10) * pow(2, i);
            ++i;
            binaryNumber /= 10;
        }
    
        i = 1;
    
        while (decimalNumber != 0) {
            octalNumber += (decimalNumber % 8) * i;
            decimalNumber /= 8;
            i *= 10;
        }
    
        return octalNumber;
    }
    
    int main() {
        long long binaryNumber = 1010;
        printf("Octal representation: %o\n", binaryToOctal(binaryNumber));
    
        return 0;
    }
    
    • Converts a binary number to its octal representation.
  6. Decimal to hexadecimal conversion in C program:

    #include <stdio.h>
    
    int main() {
        int decimalNumber = 15;
        printf("Hexadecimal representation: %X\n", decimalNumber);
    
        return 0;
    }
    
    • Converts a decimal number to its hexadecimal representation.
  7. C program for binary to hexadecimal conversion:

    #include <stdio.h>
    
    void binaryToHexadecimal(long long binaryNumber) {
        char hexaDecimal[20];
        int i = 0, remainder;
    
        while (binaryNumber != 0) {
            remainder = binaryNumber % 16;
            if (remainder < 10)
                hexaDecimal[i++] = remainder + 48;
            else
                hexaDecimal[i++] = remainder + 55;
    
            binaryNumber /= 16;
        }
    
        printf("Hexadecimal representation: ");
        for (int j = i - 1; j >= 0; --j)
            printf("%c", hexaDecimal[j]);
    
        printf("\n");
    }
    
    int main() {
        long long binaryNumber = 1010;
        binaryToHexadecimal(binaryNumber);
    
        return 0;
    }
    
    • Converts a binary number to its hexadecimal representation.