3Byte Conversion

Joined
Jan 28, 2009
Messages
9
Location
AZ
Programming Experience
Beginner
Believe it or not have a 3bytes that I want to convert to a unsigned 24 bit integer in VB. Anyone know how to do this in .net?
 
Believe it or not have a 3bytes
I don't know what that means. Do you just mean that you've got three Bytes?

Also, there's no such thing as a 24-bit number in VB. You can have a Byte, UShort, UInteger or ULong, which are 8, 16, 32 and 64 bits wide respectively.

To combine multiple Byte values into a single larger value you can use bit shifting and bitwise combinations, e.g.
VB.NET:
Dim str1 As String = "10101010"
Dim str2 As String = "11110000"
Dim str3 As String = "01010101"

Dim byte1 As Byte = Convert.ToByte(str1, 2)
Dim byte2 As Byte = Convert.ToByte(str2, 2)
Dim byte3 As Byte = Convert.ToByte(str3, 2)

MessageBox.Show(byte1.ToString(), "byte1")
MessageBox.Show(byte2.ToString(), "byte2")
MessageBox.Show(byte3.ToString(), "byte3")

Dim uint1 As UInteger = CUInt(byte1) << 16
Dim uint2 As UInteger = CUInt(byte2) << 8
Dim uint3 As UInteger = CUInt(byte3)
Dim uint4 As UInteger = uint1 Or uint2 Or uint3

MessageBox.Show(uint4.ToString(), "uint4")

Dim str4 As String = Convert.ToString(uint4, 2)

MessageBox.Show(str4, "str4")
Take a good look at the final binary string and compare it to the first three. See the similarity, i.e. it's just as though the original three were concatenated.
 
Yea I know VB doesnt support the 24 Byte integer which is why this was such a pain in the ars. Your Code did work but I found an easier approach I treated it as a 32 bit integer and just ignored the most significant byte.
 
Back
Top