Friday, December 18, 2020

C# - How to convert short array/list into byte array list

/* source with short values */
public List<short> Data = new List<short>();
...
short[] DataArray = Data.ToArray();
byte[] bufferBytes = new byte[ DataArray.Length * sizeof( short ) ];
Buffer.BlockCopy( DataArray, 0, bufferBytes, 0, bufferBytes.Length );
      
/* dest with byte values */      
Console.WriteLine( bufferBytes[0] + ", " + bufferBytes[1] );
If first short value in source list is 313, then first two values in bufferBytes after copy will be 57 (111001) a 1(1).

No comments:

Post a Comment