Is it possible to Convert Int to Hexdecimal without using 'printf'?
Best if the all the value are placed in the variable itself and some sample code with explanation.
The decimal and hexadecimal systems are just ways of expressing the value of the int. In a way "it is already a hexadecimal".
I think you can use itoa in stdlib.h :
char * itoa ( int value, char * str, int base ); or sprintf(str,"%x",value);
The documentation : itoa documentation
Of course it is possible. Just think about how printf itself was originally implemented...
I won't give you a full solution, only hints, based on which you can experiment with the implementation in code:
An 8-bit number can be expressed as 2 hex digits, which contain the higher and lower 4 bits, respectively. To get these bits, you can use the bit-shift (>>) and mask (&) operators.
Once you have a 4-bit value, you can easily map it to the correct hex digit using a sequence of ifs, or (as a more elegant solution) by indexing into a character array.
Hexdecival vs Decimal vs Binary..etc.. are only different bases that represent the same number. printf doesn't convert your number, it generates an hexdecimal string representation of your number. If you want to implement your own study how to make a conversion between decimal and hexdecimal bases.
Yes, it is definitely possible to convert an integer to a hexadecimal string without using the "printf" family of formatting functions.
You can write such a function by converting the number from base-10 (as we think about it) to base-16 (hexadecimal) and printing the digits in that representation (0-9A-F). This will require you to think a lot about the bases we use to represent numbers.
it's an example for convert a char array to hex string format
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
unsigned char d=255;
char hex_array[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char *array_to_hex_string(uint8_t data[],int size);
char test_data[3] = {'0',188,255};
int main()
{
printf("%s",array_to_hex_string(test_data,3));
return 0;
}
char *array_to_hex_string(uint8_t data[],int size){
int i,j;
char *hex_string = (char *)malloc((2*size) * sizeof(data[0]));
for(i=0;i<size;i++){
hex_string[j] = hex_array[(data[i]>>4)];
hex_string[j+1] = hex_array[(data[i] & 15)];
j +=2;
}
return (char *)hex_string;
}
int Convert2Hex(int i){
int c =0;int y, z; double a,x,b, fractpart, intpart; char nc[10]= {'0','1','2','3','4',
'5','6','7','8','9'};
char hc[6]= {'A','B','C','D','E','F'};
char tmp; //holds the value in the array
char result[20]; //string to hold first directional
char result2[20]; //string to hold second directional
intpart = 1; //intitialize intpart
double d= 16;
x=i; //pass function argument to local variable
while (intpart != 0) { //while the quotiont of the equation
//isnt 0, keep on dividiing
a = x / d; //divide the integer by 16 a,x,d all
//habe to be doubles
fractpart = modf (a, &intpart); //from the fractoral
b = (fractpart * 16); //multiply by 16 to get hex value
y = (int) b; //convert b double into int y
if (y <= 9) {
result[c] = nc[y]; //if block handles 0-9 hex
}
if (y>9){
z = b - 9; //block handles A-F hex
result[c]= hc[z-1];
}
c++; //increment the index count
x = intpart; //set the integral to be the next number chunk
}
//REVERSE THE ARRAY
int w, f;
f=c-1;
for (w = 0; w <= c;w++) {
result2[w] = result[f];
f--;
}
result2[c+1] = '\0';
printf("0x%s\n", result2);
}
cout << hex << intvar << endl;
But if you want an answer that gives you an A for your homework, you're not going to get lucky :)
printf- you're just formatting it's output.