Search code examples
vb.net16-bit8-bit

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


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?


Solution

  • 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).