Ricevo un numero di porta come 2 byte (il byte meno significativo prima) e voglio convertirlo in un numero intero in modo che possa lavorare con esso. Ho fatto questo:Converti 2 byte in un numero intero
char buf[2]; //Where the received bytes are
char port[2];
port[0]=buf[1];
port[1]=buf[0];
int number=0;
number = (*((int *)port));
Tuttavia, c'è qualcosa di sbagliato perché non ottengo il numero di porta corretto. Qualche idea?
la tua endianità è la stessa? –
anche 2 byte vs 4 byte: short vs int –
usa uint16_t per eseguire il cast –