C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
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:
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);
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);
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);
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);
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);
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.
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; }
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; }
Octal to decimal conversion in C language:
#include <stdio.h> int main() { int octalNumber = 075; printf("Decimal representation: %d\n", octalNumber); return 0; }
Hexadecimal to decimal conversion in C:
#include <stdio.h> int main() { int hexadecimalNumber = 0xA; printf("Decimal representation: %d\n", hexadecimalNumber); return 0; }
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; }
Decimal to hexadecimal conversion in C program:
#include <stdio.h> int main() { int decimalNumber = 15; printf("Hexadecimal representation: %X\n", decimalNumber); return 0; }
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; }