I have a cryptographic program in which I need to represent each character by its hex value and return all of these hex values as a char string. After multiple crypto functions I got a final decimal value of each char
. Now at the end of a for cycle I need to put all of them in a string containing each character hex value separated by space. In short, I have decimal number, which I need to convert to hex and put them in a string for return. Thanks for responses. I tried sprintf()
but I obviously can't put it into char string because it can only hold one char per index. I am a university student and its my first year working with C so please keep that in mind. I hope it is not such noob question.
I tried something.
at the top before loop starts I wrote this:
char*res=calloc(size,sizeof(unsigned char));
and before the end of loop i wrote this:
sprintf(&res[i],"%02x",dec);
dec - decimal value of a character. if i print it out i get the right output.
now my output should be "80 9c 95 95 96 11 bc 96 b9 95 9d 10"
but instead I got "899991b9b9910
".
it means i got only 1st character of each hex value except the last one. im sure now you might get a solution for me. THANK YOU!