Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. [VB.NET 2008] How to get single bytes from a multibyte value

[VB.NET 2008] How to get single bytes from a multibyte value

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncsharptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_9496613
    wrote on last edited by
    #1

    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 Single

    SingleVal = 0.125

    b1 = SingleVal And &HFF
    b2 = (SingleVal >> 8) And &HFF
    b3 = (SingleVal >> 16) And &HFF
    b4 = (SingleVal >> 24) And &HFF

    I'm not sure that the >> operator works in the right way on a Single value. What is the right way to do this? Thanks to all.

    L A 2 Replies Last reply
    0
    • S steve_9496613

      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 Single

      SingleVal = 0.125

      b1 = SingleVal And &HFF
      b2 = (SingleVal >> 8) And &HFF
      b3 = (SingleVal >> 16) And &HFF
      b4 = (SingleVal >> 24) And &HFF

      I'm not sure that the >> operator works in the right way on a Single value. What is the right way to do this? Thanks to all.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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![^]

      S 1 Reply Last reply
      0
      • L Lost User

        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![^]

        S Offline
        S Offline
        steve_9496613
        wrote on last edited by
        #3

        Thank you very much Eddy, it works:

        Dim b() As Byte
        Dim SingleVal As Single

        SingleVal = 0.125

        b = BitConverter.GetBytes(SingleVal)

        As always your answers are very helpful! :thumbsup:

        L 1 Reply Last reply
        0
        • S steve_9496613

          Thank you very much Eddy, it works:

          Dim b() As Byte
          Dim SingleVal As Single

          SingleVal = 0.125

          b = BitConverter.GetBytes(SingleVal)

          As always your answers are very helpful! :thumbsup:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Thanks :)

          1 Reply Last reply
          0
          • S steve_9496613

            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 Single

            SingleVal = 0.125

            b1 = SingleVal And &HFF
            b2 = (SingleVal >> 8) And &HFF
            b3 = (SingleVal >> 16) And &HFF
            b4 = (SingleVal >> 24) And &HFF

            I'm not sure that the >> operator works in the right way on a Single value. What is the right way to do this? Thanks to all.

            A Offline
            A Offline
            Alan N
            wrote on last edited by
            #5

            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.

            S 1 Reply Last reply
            0
            • A Alan N

              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.

              S Offline
              S Offline
              steve_9496613
              wrote on last edited by
              #6

              Yes, you're right, the value was converted to the integer type. BitConverter works. Thank you.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups