[VB.NET 2008] How to get single bytes from a multibyte value
-
Hi all, I need to extract each byte from a 32 bit value. I did something like this but it doesn't work:
Dim b1, b2, b3, b4 As Byte
Dim SingleVal As SingleSingleVal = 0.125
b1 = SingleVal And &HFF
b2 = (SingleVal >> 8) And &HFF
b3 = (SingleVal >> 16) And &HFF
b4 = (SingleVal >> 24) And &HFFI'm not sure that the
>>
operator works in the right way on aSingle
value. What is the right way to do this? Thanks to all. -
Hi all, I need to extract each byte from a 32 bit value. I did something like this but it doesn't work:
Dim b1, b2, b3, b4 As Byte
Dim SingleVal As SingleSingleVal = 0.125
b1 = SingleVal And &HFF
b2 = (SingleVal >> 8) And &HFF
b3 = (SingleVal >> 16) And &HFF
b4 = (SingleVal >> 24) And &HFFI'm not sure that the
>>
operator works in the right way on aSingle
value. What is the right way to do this? Thanks to all.steve_9496613 wrote:
What is the right way to do this?
Don't know if it helps, but there's a
[BitConverter](http://msdn.microsoft.com/en-us/library/de8fssa4.aspx)[[^](http://msdn.microsoft.com/en-us/library/de8fssa4.aspx "New Window")]
available. There's an example of it's usage on the same page.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]
-
steve_9496613 wrote:
What is the right way to do this?
Don't know if it helps, but there's a
[BitConverter](http://msdn.microsoft.com/en-us/library/de8fssa4.aspx)[[^](http://msdn.microsoft.com/en-us/library/de8fssa4.aspx "New Window")]
available. There's an example of it's usage on the same page.Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]
Thank you very much Eddy, it works:
Dim b() As Byte
Dim SingleVal As SingleSingleVal = 0.125
b = BitConverter.GetBytes(SingleVal)
As always your answers are very helpful! :thumbsup:
-
Thank you very much Eddy, it works:
Dim b() As Byte
Dim SingleVal As SingleSingleVal = 0.125
b = BitConverter.GetBytes(SingleVal)
As always your answers are very helpful! :thumbsup:
-
Hi all, I need to extract each byte from a 32 bit value. I did something like this but it doesn't work:
Dim b1, b2, b3, b4 As Byte
Dim SingleVal As SingleSingleVal = 0.125
b1 = SingleVal And &HFF
b2 = (SingleVal >> 8) And &HFF
b3 = (SingleVal >> 16) And &HFF
b4 = (SingleVal >> 24) And &HFFI'm not sure that the
>>
operator works in the right way on aSingle
value. What is the right way to do this? Thanks to all. -
I think you've fallen foul of Basic's implicit type conversions which sometimes don't help at all. The Single value 0.125 was converted to the integer 0 and it was all downhill from there! As Eddy's pointed out, BitConverter is the way to go. Alan.
Yes, you're right, the value was converted to the integer type. BitConverter works. Thank you.