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
Printable View
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
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)
What exactly are you saying?
Just sharing what a byte actually is.
You could do this though.
VB Code:
Option Explicit Private Sub Form_Load() MsgBox Format("11010011", "00 00 00 00") End Sub
that stores each pieve in value(1) value(2) value(3) value(4)VB Code:
Dim value(1 To 4) As String value(1) = Left("01001011", 2) value(2) = Right(Left("01001011", 4), 2) value(3) = Right(Left("01001011", 6), 2) value(4) = Right("01001011", 2)
This should be what you want
VB Code:
Option Explicit Private Sub Form_Load() Dim str As String Dim arr() As String Dim x As Integer str = "11010011" arr() = Split(Format(str, "00 00 00 00"), " ") str = "" For x = 0 To 3 Select Case arr(x) Case "00" str = str & "0 " Case "01" str = str & "1 " Case "10" str = str & "2 " Case "11" str = str & "3 " End Select Next x MsgBox str End Sub
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.
This might be closer. You might want to use AND/OR to do bit manipulation, though.
VB Code:
Option Explicit Private Sub Form_Load() Debug.Print BinStrToLong("1110") & " xor " & BinStrToLong("1001") & " = " & BinStrToLong("0111") End Sub Public Function BinStrToLong(ByVal BinStr As String) As Long Dim L As Long, K As Long BinStr = Right(String(32, "0") & BinStr, 32) For K = 32 To 1 Step -1 If Mid(BinStr, 32 - K + 1, 1) = "1" Then L = L + 2 ^ (K - 1) End If Next K BinStrToLong = L End Function
What are you doing exactly?
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.
So, 4 numbers from 0-3 ?
Yes sir.
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.
It's actually pretty simple math.That will return a byte array with the 4 values. Here's how to use itVB Code:
Public Function SplitByte(bValue As Byte) As Byte() Dim bArr() As Byte ReDim bArr(3) bArr(0) = (bValue And &HC0) \ &H40 bArr(1) = (bValue And &H30) \ &H10 bArr(2) = (bValue And &H8) \ &H4 bArr(3) = (bValue And &H3) SplitByte = bArr End FunctionVB Code:
Private Sub Form_Load() Dim bArr() As Byte Dim i As Integer bArr = SplitByte(75) For i = 0 To 3 Debug.Print bArr(i) Next End Sub
Slight problem, there, JA. 255 comes out as 3 3 2 3
Oh yeah, sorry my bad. Here's the correct version.VB Code:
Public Function SplitByte(bValue As Byte) As Byte() Dim bArr() As Byte ReDim bArr(3) bArr(0) = (bValue And &HC0) \ &H40 bArr(1) = (bValue And &H30) \ &H10 bArr(2) = (bValue And [b]&HC[/b]) \ &H4 bArr(3) = (bValue And &H3) SplitByte = bArr End Function
Thanks, that's what I needed.