2. Integer <-> Hex String Manipulation (a) Write the C function itox() to
ID: 3591708 • Letter: 2
Question
2. Integer <-> Hex String Manipulation
(a) Write the C function itox() to covert an integer to a hex string. Its prototype is described in the
header file xbits.h in this directory. Put your code in file xbits.c in the hw3 subdirectory of your cs240
directory. You can use the stub xbits.c from this directory to start with.
The idea behind the itox function is to convert an int variable (int is 32 bits on our machine) directly to
an ascii string of hex numbers, '0','1'…’F’. In this exercise, we will only deal with positive integers.
The algorithm to covert decimal number to hex is to first divide the int number by 16, the remainder will
form the first hex digit (the last character in the string). Use ‘0’ to represent reminder = 0; ‘1’ for
remainder =1; ‘A’ for remainder =10; ‘B’ for remainder =11 and so on. Next, divide the quotient by 16
and the remainder will form the second hex digit (next to the last). This process is repeated until the
quotient is equal to 0 or all the characters in the string have been filled up. Then the algorithm stops. If
the algorithm stops with quotient equal to 0, it fills the rest of the hex string with ‘0’. Print out the input
integer number and its corresponding hex string to see if it is correct. Remember to terminate the string
with ‘’.
Note that the array hexstring declared by the caller can be declared with a size 2*sizeof(int) + 1. Now
sizeof() is a function evaluated at precompile time which gives the number of BYTES in a variable of a
given type -- 4 bytes to an int on our machine, but it might be 8 bytes on a different machine and sizeof()
would reflect that. There are 8 bits to a byte and so we expect 2*sizeof(int) chars ‘0’, ‘1’,…’F’ in a
representation of the hex value. The +1 is to leave space for the final '' in the string.
(b) Write the C function xtoi() to convert a hex string to an integer. Its prototype is also described in
xbits.h. Put your code in file xbits.c in the hw3 subdirectory of your cs240 directory. Your algorithm
should only accept the valid hex digit 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F.
The algorithm to convert hex string to a decimal number is to start the string from the last character.
Remember to skip over the ‘’ character. Convert the corresponding character to its decimal equivalent
(e.g. a character A will become 10) and multiply the number by powers of 16. The decimal equivalent of
the last character will be multiplied by 16 0 ; the next by 16 1 and so on. The result is obtained by summing
all the products together. Print out the hex string and its equivalent decimal number to see if it is correct.
NOTE: The functions in xbits.c must be called by another program, and will not generate any output. So
to test them you will have to write a driver (a main program that calls these functions and with
appropriate arguments and outputs results -- this should be named showxbits.c). You must then compile
the driver and link the compiled driver object code with the object code for the functions you write,
combining them into a single executable. Note that there is a stub showxbits.c driver you can use to test
the program and the code in xbits.c and xbits.h. To test bits initially, you can give the command to build
and run the showxbits program:
gcc showxbits.c xbits.c -o showxbits
./showxbits
The xbits.h file is called a header file. The header file is the glue that makes sure xbits.c and the driver
have the same prototypes for the functions in xbits.c. It guarantees that any correct driver can link with
any correct implementation.
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;
}
------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.