im writing a code for converting a number to hexadecimal and im getting a random numbers as result.
at first i succeed to convert the number but it was in a reversed order (the first mod needs to be the last number or letter).
the code is a part (for cases of %x
) from a big project that is kind of implementation of sprintf
(so sprintf
or printf
are not allowed obviously). so the buffer
is for composing a string without any placeholders.
thank u in advance.
here's my code:
int num = *(int*)ptrs[counter];
int tempnum=num;
int mod=0;
int length =0;
for(int i=0;tempnum !=0;i++)
{
length++;
tempnum /= 16;
}
int array[length];
for(int i= length; i>0;i--)
{
mod = num%16;
num = num/16;
array[i] = mod;
}
for(int i=0;i<length;i++)
{
if(array[i]<10)
*buffer = array[i]+ '0';
else
*buffer = array[i] -10 + 'a';
buffer++;
}