I have a unsigned short buffer , it contain a hexa values of IP header .
unsigned short buffer = {45,00,00,4E,4E,05,00,10,80,11,0X00,0X00,0A,00,00,1C,1A,00,00,2F};
I added 0X00 , 0X00
in the place of Checksum filed and calculated checksum with the RFC 1071 - Calculating IP header checksum . So it is returning a unsigned short checksum
value . Now i need to insert to the Ip buffer .
For example : if my unsigned short checksum value
= 55168
and it's Hex value = D780
. Now i need to add D7
, 80
to buffer[11]
and buffer[12]
. How can i convert unsigned short checksum value and split the Hex value for inserting to the buffer ? If it Hex = D78
, then also i need to add 0D
and 78
to the buffer field .
You can separate the values using the bit operators
buffer[11] = (checksum >> 8) & 0xff;
buffer[12] = checksum & 0xff;
The first line shifts the checksum by 8 bits to the right, which is equivalent to dividing by 256 and than ensures, that you only get the information of those remaining 8 bits (0xD7) by using the and(&) operator with 0xff which equals to 1111 1111 in binary representation. The second line also uses the and operator to save ONLY the last 8 bit of your checksum, which would be 0x80.