I want to convert unsigned char to hex (using unsigned int). This is my code so far. I have a program1 that produces an unsigned char array and the other program2 only takes in only hex (using unsigned int), so what i want to achieve is getting an input of unsigned char array and converting that array into hex.
(E.g., program1 outputs "1234567812345678", program2 should output "31323334353637383132333435363738")
Sorry if this question seems dumb. Looked around for answers here but it didn't seem to be what I wanted.
uint64_t phex (unsigned char[16], long);
int main (void) {
int i;
unsigned char key[16] = "1234567812345678";
uint64_t* keyHex = phex(key,16); //store into an array here
for(i = 0; i < 16; i++)
printf("%.2x ", keyHex[i]);
free(keyHex);
return 0;
}
uint64_t phex(unsigned char* string, long len)
{
int i;
//allocate memory for your array
uint64_t* hex = (uint64_t*)malloc(sizeof(uint64_t) * len);
for(i = 0; i < len; ++i) {
//do char to int conversion on every element of char array
hex[i] = string[i] - '0';
}
//return integer array
return hex;
}
malloc
in C – phuclv0x1234567812345678
in hexadecimal is1311768465173141112
in decimal. I don't know where you get31323334353637383132333435363738
. Maybe you want to convert binary data to hexadecimal string representation? – Barmak Shemirani