Here is my little simple function before fixing:
GeSHi (c):
void charStrToHexStr(char* t, int size, char* hexStrEncrypted)
{
int i; //Index of input
char c[2]; //Temporary to hold hex value
hexStrEncrypted[0] = '\0';
for(i = 0; i < size; i++) //Iterate through input
{
sprintf(c, "%02X", t[i]&0xFF); //Convert input into hex and put into c[2].
//1 char will have 2 hex digit max so c[0] holds higher order
//hex (0xF0) and c[1] holds lower order hex (0x0F)
strcat(hexStrEncrypted, c); //Append c to hexStrEncrypted
}
hexStrEncrypted[i*2+1] = '\0'; //Terminate output with null
}
Created by GeSHI 1.0.7.18
The purpose is just to convert a char into it's hex equivalent but as a string. So char 'a' will be converted into string "41".
The problem is if input string t is longer than 1, I get into infinite loop with counter i gets reset to 0 on every iteration. What a weird behaviour!
The problem is fixed buy changing size of char c[] to 3 instead of 2. I can't think WHY. c[2] should be enough to hold the hex digits which will be at most 2 per char. c[0] = first hex, c[1] = second hex, c[2] = null.
Why?
