
- Learn C By Examples Time
- Learn C by Examples - Home
- C Examples - Simple Programs
- C Examples - Loops/Iterations
- C Examples - Patterns
- C Examples - Arrays
- C Examples - Strings
- C Examples - Mathematics
- C Examples - Linked List
- C Programming Useful Resources
- Learn C By Examples - Quick Guide
- Learn C By Examples - Resources
- Learn C By Examples - Discussion
Octal To Hexadecimal program in C
Implementation
Implementation of this algorithm is given below −
#include <stdio.h> int string_length(char s[]) { int i=0; while(s[i]!='\0') i++; return i; } int octal2decimal(char n[]) { int i,decimal,mul=0; for(decimal=0,i=string_length(n)-1;i>=0;--i,mul+=3) decimal+=(n[i]-48)*(1<<mul); return decimal; } int main() { char n[] = "7"; char bits[100]; int i,j,d; string_reverse(n); for(i=j=0;n[i]!='\0';++i) for(d=n[i]-48;d!=0;d/=2) bits[j++]=d%2+48; bits[j]='\0'; string_reverse(bits); printf("Binary equivalent of %s is : %s\n", n, bits); return 0; }
Output
Output of the program should be −
Binary equivalent of 7 is : 111
Advertisements