Question

Using the C programming language, write a program that takes in hexadecimal characters (using the fgets()...

Using the C programming language, write a program that takes in hexadecimal characters (using the fgets() function) and return a translated string of ASCII characters. Use only BITWISE operators, not arithmetic.

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

#include <stdio.h>
#include <string.h>

//function to convert from hexa to decimal
int hexaToDecimal(char c)
{
int first = c;
first = first >> 4;
first = first - 3;
int second = c;
second = second & 0x0F;
  
int decimal = (first<<1 + first<<3) + second;

if(decimal >= 10)
decimal--;
return decimal;
}

//fuction to convert from hexa to ascii
int hexaToASCII(char firstHexa, char secondHexa)
{
int highByte = hexaToDecimal(firstHexa) << 4;
int lowByte = hexaToDecimal(secondHexa);
return highByte | lowByte;
}

int main()
{
//variable declaration
char hexa[100];// = "41";
printf("Enter hex number: ");
fgets(hexa,sizeof(hexa),stdin);
  
//get string length
int strLength = strlen(hexa);
  
//display output
printf("\nThe equivalent ASCII string is: ");
for(int i = 0; i < strLength-1; i++)
{
printf("%c", hexaToASCII(hexa[i], hexa[i+1]));
i++;
}
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Using the C programming language, write a program that takes in hexadecimal characters (using the fgets()...
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