Question

In: Computer Science

A byte is made up of 8 bits although a byte is rarely represented as a...

A byte is made up of 8 bits although a byte is rarely represented as a string of binary digits or bits. Instead, it is typically presented as a pair of hexadecimal digits where each digit represents 4 bits or half a byte or "nibble." We have been reading in hex digits and outputting their decimal value, but what we really want to do is create a byte from a pair of hex digits read from input.

Create a C program that:

  1. Reads a pair of hexadecimal characters from input. For example: 3B or CF.
  2. Converts each of them into their decimal value. For example: 3 (0011 in binary) and 11 (1011 in binary)
  3. Packs the two nibbles into a single byte. For example, 3 and B packed into an 8 bit byte would be 0011 1011.
  4. Outputs the merged byte in hexadecimal to verify that the byte was packed correctly.

The printf() function supports outputting an integer as a hexadecimal number using the %x (where the hexadecimal letters are presented in lower-case) and %X (where the hexadecimal letters are presented in upper-case).

To pack two integers that have values represented with only 4 bits, you need to perform two operations with the integers:

  1. Shift the integer holding the decimal value of the first hexadecimal digit in the pair to the left 4 bits.
  2. OR the integer holding the decimal value of the second hexadecimal in the pair with the shifted integer.

After the shift and the OR, the first integer will not have the lower 8 bits (byte) set to the values represented by the two characters read from input.

Solutions

Expert Solution

// C program to convert a pair of hexadecimal characters to BYTE

#include <stdio.h>

#include <string.h>

int main()

{

    char hexa[2], byte[8] = "";

    int i,n=0,l;

    /* Enter a pair of hexadecimal digts from user */

    printf("Enter a pair of hexadecimal digits: ");

    x:gets(hexa);

    l=strlen(hexa);

    if (l>2)

    {printf(" Please enter only two hexadecimal digits");

    goto x;

     }

    /*loop to extract digit and find binary of each hex digit */

    for(i=0; hexa[i]!='\0'; i++)

    {

        switch(hexa[i])

        {

            case '0':

                strcat(byte, "0000");

                break;

            case '1':

                strcat(byte, "0001");

                break;

            case '2':

                strcat(byte, "0010");

                break;

            case '3':

                strcat(byte, "0011");

                break;

            case '4':

                strcat(byte, "0100");

                break;

            case '5':

                strcat(byte, "0101");

                break;

            case '6':

                strcat(byte, "0110");

                break;

            case '7':

                strcat(byte, "0111");

                break;

            case '8':

                strcat(byte, "1000");

                break;

            case '9':

                strcat(byte, "1001");

                break;

            case 'a':

            case 'A':

                strcat(byte, "1010");

                break;

            case 'b':

            case 'B':

                strcat(byte, "1011");

                break;

            case 'c':

            case 'C':

                strcat(byte, "1100");

                break;

            case 'd':

            case 'D':

                strcat(byte, "1101");

                break;

            case 'e':

            case 'E':

                strcat(byte, "1110");

                break;

            case 'f':

            case 'F':

                strcat(byte, "1111");

                break;

            default:

                n=1;    

}

    }

if(n==0)

    {

    printf("Hexademial Pair = %s\n", hexa);

    printf("Byte Conversion = %s", byte);

    }

else

    printf("Wrong hexadecimal input");

    return 0;

}


Related Solutions

#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[])...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] & b[i]; } } void func_or(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] || b[i]; } } void func_not(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i]...
Design a 1-bt eror correction code for m=8 data bits and r=4 check bits. The 8...
Design a 1-bt eror correction code for m=8 data bits and r=4 check bits. The 8 data bits are 10101011. For odd parity,assign the 4 check bits ,and give the 22 bits CODE WORD.
A family of four that is made up from a 2 and 8 year old plus...
A family of four that is made up from a 2 and 8 year old plus their parents were traveling from Europe to the USA. On the way it was noticed that the 8 year old boy is having fever, headache and his head was tilting to the right in addition there was some form of rash developing on his hands and feet. Their father was feeling very fatigued with flu like symptoms that included a headache and tilting of...
Latches are commonly organized into groups of 4-bits, 8-bits or more into registers. 1 point True...
Latches are commonly organized into groups of 4-bits, 8-bits or more into registers. 1 point True False An asynchronous counter is one where all stages of the counter trigger together. 1 point True False Pull-up resistors and special interface ICs are typically used as an interface between different logic families. 1 point True False Which logic gate provides a low output only when an odd number of inputs are high? 1 point and or exclusive OR exclusive NOR Which of...
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Implement a recursive binary search on an array of 8-byte integers in LEGV8
December 1: Anne made an investment in Byte of Accounting, Inc., by purchasing 2,000 shares of...
December 1: Anne made an investment in Byte of Accounting, Inc., by purchasing 2,000 shares of its common stock paying $48,000.00 in cash.  The par value of the common stock was $22.00 per share. December 3:  Purchased a Ricoh Color copier for $5,200.00.  The invoice number was 61298.  We paid 10% and signed a 3 year note for the remaining balance.   Interest at a rate of 6% a year will be paid semiannually. December 3:   Check # 6001  for $2,600.00 was paid for rent of the office...
IEEE 754 format of 32-bit floating-point is as follows. 1 8 (bits) 23 (bits) What’s stored...
IEEE 754 format of 32-bit floating-point is as follows. 1 8 (bits) 23 (bits) What’s stored in each region? What’s the bias value and how to get it? For decimal fraction: – 0.625, please represent it as given format (Note: you must show the specific procedure/stepsin order to get full credits. If you only present final result directly, you will only get half of the credits even if it is correct.).  
A direct-mapped cache consists of 8 blocks. Byte-addressable main memory contains 4K blocks of 8 bytes...
A direct-mapped cache consists of 8 blocks. Byte-addressable main memory contains 4K blocks of 8 bytes each. Access time for the cache is 22ns, and the time required to fill a cache slot from main memory is 300ns. (This time allows us to determine the block is missing and bring it into cache.) Assume a request is always started in parallel to both cache and to main memory(so if it is not found in cache, we do not have to...
A direct-mapped cache consists of 8 blocks. Byte-addressable main memory contains 4K blocks of 8 bytes...
A direct-mapped cache consists of 8 blocks. Byte-addressable main memory contains 4K blocks of 8 bytes each. Access time for the cache is 22ns, and the time required to fill a cache slot from main memory is 300ns. (This time allows us to determine the block is missing and bring it into cache.) Assume a request is always started in parallel to both cache and to main memory(so if it is not found in cache, we do not have to...
Make a schema for a memory size of 65536 words per 8 bits. Use 8k  by 8-bit...
Make a schema for a memory size of 65536 words per 8 bits. Use 8k  by 8-bit 2764 memory a) with a single decoder 388 and inverters. b) Use a decoder 2 4 and inverters? Help?   How to implement?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT