Results 1 to 5 of 5

Thread: [RESOLVED] Need math help, extracting bytes from Long

  1. #1

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Resolved [RESOLVED] Need math help, extracting bytes from Long

    I need to pack four individual Byte variables into a single Long variable, and then be able to split that Long back into its four constituent Bytes. (For use in a Listbox ItemData property.)

    I haven't a clue where to begin. Anyone know? Speed/efficiency isn't mission critical, but elegance is always appreciated.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Need math help, extracting bytes from Long

    The 4th byte is the tricky part unless it's value is never > 127.

    Creating packed Long
    1st byte: lValue = lValue Or Byte
    2nd byte: lValue = lValue Or (Byte * &H100&)
    3rd byte: lValue = lValue Or (Byte * &H10000)
    4th byte
    -- < 128? lValue = lValue Or (Byte * &H1000000)
    -- > 127? lValue = lValue Or ((Byte XOR &H80) * &H1000000) Or &H80000000

    Extraction:
    1st byte: Byte = lValue And &HFF&
    2nd byte: Byte = (lValue And &HFF00&) \ &H100&
    3rd byte: Byte = (lValue And &HFF0000) \ &H10000
    4th byte:
    -- lValue non-negative? Byte = (lValue And &HFF000000) \ &H1000000
    -- lValue is negative? Byte = (lValue And &H7F000000) \ &H1000000 Or &H80
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Need math help, extracting bytes from Long

    more cat skinning
    Code:
    Option Explicit
    
    Private Type tByte4
      b1 As Byte
      b2 As Byte
      b3 As Byte
      b4 As Byte
    End Type
    
    Private Type tLong
      n As Long
    End Type
    
    Private Function Pack(ByVal byts As tByte4) As Long
      Dim lng As tLong
      LSet lng = byts
      Pack = lng.n
    End Function
    
    Private Function UnPack(ByVal number As Long) As tByte4
      Dim lng As tLong
      lng.n = number
      LSet UnPack = lng
    End Function
    
    Private Function Pack2(ByVal b1 As Byte, ByVal b2 As Byte, ByVal b3 As Byte, ByVal b4 As Byte) As Long
      Dim byts As tByte4, lng As tLong
      byts.b1 = b1
      byts.b2 = b2
      byts.b3 = b3
      byts.b4 = b4
      LSet lng = byts
      Pack = lng.n
    End Function
    
    Private Function Pack3(ByVal b1 As Byte, ByVal b2 As Byte, ByVal b3 As Byte, ByVal b4 As Byte) As Long
      Pack = b1 Or b2 * &H100& Or b3 * &H10000
      Pack = Pack Or (b4 And &H7F&) * &H1000000 Or (b4 And &H80&) * &HFF000000
    End Function
    
    Private Function UnPack2(ByVal number As Long) As tByte4
      With UnPack
        .b1 = (number And &HFF&)
        .b2 = (number And &HFF00&) \ &H100&
        .b3 = (number And &HFF0000) \ &H10000
        .b4 = (number And &H7F000000) \ &H1000000 Or (number And &H80000000) \ &HFF000000
      End With
    End Function
    W o t . S i g

  4. #4

    Thread Starter
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Need math help, extracting bytes from Long

    Cool, thanks. I've been googling around since I posted the OP, and I think I found a faster, more elegant way that uses just pure native VB. Plus you get the full range of values in every byte with no overflow issues. Check it out:
    vb Code:
    1. Private Type LongType
    2.     Value As Long
    3. End Type
    4.  
    5. Private Type TwoIntsType
    6.     Int1 As Integer
    7.     Int2 As Integer
    8. End Type
    9.  
    10. Private Type FourBytesType
    11.     Byte1 As Byte
    12.     Byte2 As Byte
    13.     Byte3 As Byte
    14.     Byte4 As Byte
    15. End Type
    16.  
    17. Public Sub Sample(plngTest As Long) As Long
    18.     Dim typLong As LongType
    19.     Dim typInts As TwoIntsType
    20.     Dim typBytes As FourBytesType
    21.  
    22.     Debug.Print "Long: " & plngTest
    23.    
    24.     ' Convert Long to Integers
    25.     typLong.Value = plngTest
    26.     LSet typInts = typLong
    27.  
    28.     With typInts
    29.         Debug.Print "Integers: " & .Int1, .Int2
    30.     End With
    31.    
    32.     ' Convert Integers to Bytes
    33.     LSet typBytes = typInts
    34.  
    35.     With typBytes
    36.         Debug.Print "Bytes: " & .Byte1, .Byte2, .Byte3, .Byte4
    37.     End With
    38.    
    39.     ' Convert Bytes to Long
    40.     LSet typLong = typBytes
    41.  
    42.     Debug.Print "Long: " & plngTest
    43. End Function
    Sample:
    Code:
    sample 1234567890
    Long: 1234567890
    Integers: 722  18838 
    Bytes: 210   2   150       73 
    Long: 1234567890
    Even the help file documents this. Turns out that LSet() is a native VB command that is functionally equivalent to CopyMemory for UDTs. All you need to do is make sure they're the same size.

    That's way cool.

    EDIT: Yeah, I've been playing around with this for an hour. I should probably preview before posting, eh? This post was in response to LaVolpe, not Milk. In any case, thanks much to both of you.
    Last edited by Ellis Dee; Nov 18th, 2010 at 03:53 PM.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Need math help, extracting bytes from Long

    Whichever floats your boat

    I'll probably never change my methods simply because bit shifting/masking is very comfortable for me, requires no structures, and is very fast indeed.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width