Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Integer to Hex string and Hex string to Integer (IN C only) 1 Interger to Hex St

ID: 3754559 • Letter: I

Question

Integer to Hex string and Hex string to Integer (IN C only)

1 Interger to Hex String Write the C function itox) to covert an integer to a hex string. Its prototype is described in the header file xbits.h. Write your code in the file xbits.c inyour hw3 directory. You can build from the stub xbits.c from my hw3 directory. The idea behind the itox function is to convert an int variable (int is 32 bits on our machine) 'F'. In this assignment, we will consider directly to an ASCII string of hex numbers, '0', '1', ..., ' only positive integers. The algorithm is as follows 1. Divide the int by 16. 2. The remainder is the first hex digit, to be placed in hexstring[7] Use '0' to represent remainder - 0, '1' for remainder- 1, 'A' for remainder - 10, 'B' for remainder 11, and so on 3. Update the int to be the quotient. 4. Repeat steps 1, 2, and 3 for sizeof (int) * 2 times When Step 2 is executed for the second time, the hex digit will go into hexstring[6]; when Step 2 is executed for the third time, the hex digit will go into hexstring[5], and so on 5. Terminate the hexstring with Note that the array hexstring declared by the caller can be declared with a size sizeof(int) * 2 + 1. sizeof ) is evaluated at compile time to be the number of bytes in a variable of a given type. For example, it is 4 bytes to an int on our machine, but it might be 8 bytes on a different machine. Thus sizeof () allows your code to be portable.

Explanation / Answer

xbits.h:

----------------------------------

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


int itox(int num, char *result);

int xtoi(char hexinp[], int *output);

---------------------------------

xbits.c:

--------------------------------------------------------------------------------------------------------------

#include "xbits.h"

/* Progaram to reverse the string */
char *strrev(char *str)
{
   char *p1, *p2;

   if (! str || ! *str)
       return str;
   for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
   {
       *p1 ^= *p2;
       *p2 ^= *p1;
       *p1 ^= *p2;
   }
   return str;
}

int itox(int num, char *result)
{
   int quo,remainder;
   quo = num;
   int i=0,j=0;
   //checking if the quotient is not zero
   while (quo != 0)
   {

       remainder = quo % 16;
       //if the remainder is < 10 adding 48 to remainder
       if (remainder < 10)
           result[j++] = 48 + remainder;
       else //if remainder is not less than 10 adding 55 to remainder
           result[j++] = 55 + remainder;
       quo = quo / 16;
   }
   while(j<2*sizeof(int))
   {
       result[j++] = '0';
   }

   //reversing the string
   result = strrev(result);
   result[j] = '';
   return 0;
}

int xtoi(char hexinp[], int *output)
{
   int len = strlen(hexinp);
   int base = 1;

   *output=0;
   int i=0;
   // Extracting characters as digits from last character
   for ( i=len-1; i>=0; i--)
   {
       // if character lies in '0'-'9', converting
       // it to integral 0-9 by subtracting 48 from
       // ASCII value.
       if (hexinp[i]>='0' && hexinp[i]<='9')
       {
           *output += (hexinp[i] - 48)*base;

           // incrementing base by power
           base = base * 16;
       }

       // if character lies in 'A'-'F' , converting
       // it to integral 10 - 15 by subtracting 55
       // from ASCII value
       else if (hexinp[i]>='A' && hexinp[i]<='F')
       {
           *output += (hexinp[i] - 55)*base;

           // incrementing base by power
           base = base*16;
       }
   }
   return 0;
}

-----------------------------------------------------------------------------------------------------

showbits.c:

--------------------------------------------------------------------------------------------------

#include "xbits.h"

int main()
{

    int x = 2545;
    char hexstring[2*sizeof(int)+1];

    itox(x, hexstring);
    printf("(decimal to hex) --- %d ---- %s ",x, hexstring);
    xtoi(hexstring,&x);

    printf("(hex to decimal) %s ----- %d ", hexstring, x);
    return 0;
}

------------------------------------------------------------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote