I was reading a program in C (an implementation of a server/client communication) and I saw this:
for (i = 0; i < len; i++)
sprintf(nickmsg+i*2, "%02X", buf[i] & 0xFF);
What does this line do? I don’t understand this especially: nickmsg+i*2.
nickmsg is a char table and i is an integer. If it was just nickmsg, ok I’ll understand but there what’s the aim of this line ?
Thanks.
Start at the address pointed to by
nickmsgand then go an additionali * 2 * CHAR_BIT / 8bytes in memory. From there, write the hex representation ofbuf[i] & 0xFF, which will occupy2 * CHAR_BIT / 8bytes. Repeat for eachi.Assuming
buflooks likeThen the memory pointed to by
nickmsgwill look like:Where the
\is my nomenclature for the null-terminator thatsprintfwrites at the end.