0

I am writing a tftp client. But when i convert the block number as follows:

uint16_t blockN = buffer[2]<<8 | buffer[3];

after 127, i am getting 65408 as blockN. What might be the problem here?

Thank you for your answers.

ahmetsarias
  • 241
  • 1
  • 15

2 Answers2

4

You have to change the type of buffer array from an array of char to an array of unsigned char, otherwise buffer[2] will be promoted to int and sign extension will occur. On most platforms char type is a signed type.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    Ok i solved but not exaclty this way but this idea helped. I did uint16_t blockN = buffer[2]<<8 | buffer[3]&0xFF; so i got rid of sign extensions – ahmetsarias Nov 29 '14 at 00:36
0

I solved by doing uint16_t blockN = buffer[2]<<8 | (buffer[3]&0xFF);

ahmetsarias
  • 241
  • 1
  • 15