How do I split a 16-bit value into two 8-bit values in VB.NET?

Arindam Das picture Arindam Das · Feb 27, 2013 · Viewed 14.5k times · Source

I have a 16-bit value like this:

 0000000000000011

I want to split this in two 8-bit values like:

 00000000    and    00000011

The 16-bit value is a variable, byte_val1, which is a unsigned 16-bit integer.

How can I do this in VB.NET?

Answer

Guffa picture Guffa · Feb 27, 2013

You can use the BitConverter class:

Dim bytes As Byte() = BitConverter.GetBytes(byte_val1)

Now bytes(0) contains the lower byte (00000011) and bytes(1) contains the higher byte (00000000).