2010-03-29 17 views
7

Ciao, ho un semplice problema che mi ha infastidito e posso trovare una soluzione. Ho ottenuto una matrice che contiene dati int con segno, ho bisogno di convertire ogni valore nella matrice a 2 byte. Sto usando C# e ho provato a utilizzare BitConverter.GetBytes (int) ma restituisce una matrice a 4 byte.Converti int con firma a 16 bit a 2 byte?

Qualsiasi aiuto?

grazie Tristan

risposta

15

Un firmato valore a 16-bit è meglio rappresentato come un short piuttosto che int - in modo da utilizzare BitConverter.GetBytes(short).

Tuttavia, in alternativa:

byte lowByte = (byte) (value & 0xff); 
byte highByte = (byte) ((value >> 8) & 0xff);