Results 1 to 16 of 16

Thread: Byte Splitting. [Resolved]

  1. #1

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Resolved Byte Splitting. [Resolved]

    What's the most efficient way to split a Byte into 4 two bit segments?
    For instance, if my number is 01001011 I would want four numbers, 01 (1), 00 (0), 10 (2), 11 (3). Thanks.




    Added green "resolved" checkmark - Hack
    Last edited by Hack; Oct 18th, 2005 at 07:44 AM.

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Byte Splitting.

    They are actually split into groups of 4, which equals the HEX value. Each byte is 63 32 16 4 | 8 4 2 1

    FF (255) is ALL bits ON, not F + F (32)

  3. #3

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Re: Byte Splitting.

    What exactly are you saying?

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Byte Splitting.

    Just sharing what a byte actually is.

    You could do this though.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   MsgBox Format("11010011", "00 00 00 00")
    5. End Sub

  5. #5
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Byte Splitting.

    VB Code:
    1. Dim value(1 To 4) As String
    2. value(1) = Left("01001011", 2)
    3. value(2) = Right(Left("01001011", 4), 2)
    4. value(3) = Right(Left("01001011", 6), 2)
    5. value(4) = Right("01001011", 2)
    that stores each pieve in value(1) value(2) value(3) value(4)

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Byte Splitting.

    This should be what you want

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim str As String
    5.   Dim arr() As String
    6.   Dim x As Integer
    7.   str = "11010011"
    8.   arr() = Split(Format(str, "00 00 00 00"), " ")
    9.   str = ""
    10.   For x = 0 To 3
    11.     Select Case arr(x)
    12.     Case "00"
    13.       str = str & "0 "
    14.     Case "01"
    15.       str = str & "1 "
    16.     Case "10"
    17.       str = str & "2 "
    18.     Case "11"
    19.       str = str & "3 "
    20.     End Select
    21.   Next x
    22.   MsgBox str
    23. End Sub

  7. #7

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Re: Byte Splitting.

    Thanks for your input, but simple string manipulation I can do. What I meant was how do I take an actual Byte data type and convert it into four bit groups?
    For instance, if my number is 01001011 (75, I think) I would want four numbers, 01 (1), 00 (0), 10 (2), 11 (3). Thanks.

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Byte Splitting.

    This might be closer. You might want to use AND/OR to do bit manipulation, though.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Debug.Print BinStrToLong("1110") & " xor " & BinStrToLong("1001") & " = " & BinStrToLong("0111")
    5. End Sub
    6.  
    7. Public Function BinStrToLong(ByVal BinStr As String) As Long
    8.     Dim L As Long, K As Long
    9.         BinStr = Right(String(32, "0") & BinStr, 32)
    10.    
    11.     For K = 32 To 1 Step -1
    12.         If Mid(BinStr, 32 - K + 1, 1) = "1" Then
    13.             L = L + 2 ^ (K - 1)
    14.         End If
    15.     Next K
    16.    
    17.     BinStrToLong = L
    18. End Function

    What are you doing exactly?

  9. #9

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Re: Byte Splitting.

    I'm saving four different values in one byte. This byte will be part of a file. In order to extrapolate the 4 different values from the byte, I need to seperate it into the four parts I mentioned. I'll be starting with a Byte data type, so no strings will be involved.

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Byte Splitting.

    So, 4 numbers from 0-3 ?

  11. #11

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Re: Byte Splitting.

    Yes sir.

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Byte Splitting.

    I think a lookup table would be the best way to do this. Not sure the best way to do it, though. Send a PM to Merri. He's the byte guru around here.

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Byte Splitting.

    It's actually pretty simple math.
    VB Code:
    1. Public Function SplitByte(bValue As Byte) As Byte()
    2.     Dim bArr() As Byte
    3.     ReDim bArr(3)
    4.     bArr(0) = (bValue And &HC0) \ &H40
    5.     bArr(1) = (bValue And &H30) \ &H10
    6.     bArr(2) = (bValue And &H8) \ &H4
    7.     bArr(3) = (bValue And &H3)
    8.     SplitByte = bArr
    9. End Function
    That will return a byte array with the 4 values. Here's how to use it
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim bArr() As Byte
    3.     Dim i As Integer
    4.    
    5.     bArr = SplitByte(75)
    6.     For i = 0 To 3
    7.         Debug.Print bArr(i)
    8.     Next
    9. End Sub

  14. #14
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Byte Splitting.

    Slight problem, there, JA. 255 comes out as 3 3 2 3

  15. #15
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Byte Splitting.

    Oh yeah, sorry my bad. Here's the correct version.
    VB Code:
    1. Public Function SplitByte(bValue As Byte) As Byte()
    2.     Dim bArr() As Byte
    3.     ReDim bArr(3)
    4.     bArr(0) = (bValue And &HC0) \ &H40
    5.     bArr(1) = (bValue And &H30) \ &H10
    6.     bArr(2) = (bValue And [b]&HC[/b]) \ &H4
    7.     bArr(3) = (bValue And &H3)
    8.     SplitByte = bArr
    9. End Function

  16. #16

    Thread Starter
    Fanatic Member Nove's Avatar
    Join Date
    Jul 2004
    Posts
    736

    Re: Byte Splitting.

    Thanks, that's what I needed.

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