|
-
Oct 17th, 2005, 06:17 PM
#1
Thread Starter
Fanatic Member
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.
-
Oct 17th, 2005, 06:24 PM
#2
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)
-
Oct 17th, 2005, 06:28 PM
#3
Thread Starter
Fanatic Member
Re: Byte Splitting.
What exactly are you saying?
-
Oct 17th, 2005, 06:38 PM
#4
Re: Byte Splitting.
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
-
Oct 17th, 2005, 06:40 PM
#5
Frenzied Member
Re: Byte Splitting.
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)
that stores each pieve in value(1) value(2) value(3) value(4)
-
Oct 17th, 2005, 06:48 PM
#6
Re: Byte Splitting.
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
-
Oct 17th, 2005, 07:39 PM
#7
Thread Starter
Fanatic Member
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.
-
Oct 17th, 2005, 07:45 PM
#8
Re: Byte Splitting.
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?
-
Oct 17th, 2005, 08:12 PM
#9
Thread Starter
Fanatic Member
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.
-
Oct 17th, 2005, 08:24 PM
#10
-
Oct 17th, 2005, 08:25 PM
#11
Thread Starter
Fanatic Member
-
Oct 17th, 2005, 08:52 PM
#12
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.
-
Oct 17th, 2005, 09:03 PM
#13
Re: Byte Splitting.
It's actually pretty simple math.
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 &H8) \ &H4
bArr(3) = (bValue And &H3)
SplitByte = bArr
End Function
That will return a byte array with the 4 values. Here's how to use it
VB 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
-
Oct 17th, 2005, 09:48 PM
#14
Re: Byte Splitting.
Slight problem, there, JA. 255 comes out as 3 3 2 3
-
Oct 18th, 2005, 06:02 AM
#15
Re: Byte Splitting.
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
-
Oct 18th, 2005, 06:59 AM
#16
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|