Question

Write a loop-less function to convert from Hex to Binary. (HINT: Use a helper function/recursion) binHex[16] [5] {0000, O001,0010,0011,0100 ,0101, 0110, 0111, 1000, 1001 , 1010, 1011, 1100, 1101, 1110 1 const char = s char hexToBinary ...) 7

I think you are actually doing binary to hex. please do this without loops and you can use recursion. please write a working C code. Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

const char binHex[16][5] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};

//recursive method to convert hexadecimal to decimal

char * hextoBinary(char *hex,int i,int n,char *bin)

{

if(i==n)return bin;//retunring converted binary number

else

{

switch(hex[i])

{

case '0':

strcat(bin, binHex[0]);

break;

case '1':

strcat(bin, binHex[1]);

break;

case '2':

strcat(bin, binHex[2]);

break;

case '3':

strcat(bin, binHex[3]);

break;

case '4':

strcat(bin,binHex[4]);

break;

case '5':

strcat(bin, binHex[5]);

break;

case '6':

strcat(bin, binHex[6]);

break;

case '7':

strcat(bin, binHex[7]);

break;

case '8':

strcat(bin, binHex[8]);

break;

case '9':

strcat(bin, binHex[9]);

break;

case 'a':

case 'A':

strcat(bin, binHex[10]);

break;

case 'b':

case 'B':

strcat(bin, binHex[11]);

break;

case 'c':

case 'C':

strcat(bin, binHex[12]);

break;

case 'd':

case 'D':

strcat(bin,binHex[13]);

break;

case 'e':

case 'E':

strcat(bin, binHex[14]);

break;

case 'f':

case 'F':

strcat(bin, binHex[15]);

break;

default:

printf("Invalid hexadecimal number");

}

//recursive call

return hextoBinary(hex,i+1,n,bin);

}

}

int main()

{

char hex[17], *bin= (char *)malloc(sizeof(char)*65);

int i = 0;

//taking input

printf("Enter any hexadecimal number: ");

gets(hex);

for(i=0; hex[i]!='\0'; i++);//finding size of hexadecimal number

//calling method

bin = hextoBinary(hex,0,i,bin);

//displaying output

printf("Hexademial number: %s\n", hex);

printf("Binary number: %s\n", bin+3);

return 0;

}

output:

Enter any hexadecimal number: 1a
Hexademial number: 1a
Binary number: 00011010


Process exited normally.
Press any key to continue . . .

//PLS give a thumbs up if you find this helpful, its helps me alot, thanks

Add a comment
Know the answer?
Add Answer to:
I think you are actually doing binary to hex. please do this without loops and you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT