Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I convert Bytes to bits in VB 2

Status
Not open for further replies.

RubyL

Programmer
Apr 28, 2001
14
US
I working with a video data recorder and using MSComm to read data from the port. All of that is fine, but now i need to convert the bytes I recieve from the port to bits "1" and "0" so that I can parse the data in binary and then convert the parsed data to decimal. Any suggestions. I am using VB 5.
 
I guess there must be an easier way but I guess something like this should work:

Dim instring As String, i As Integer, j As Integer, Onebyte As Byte, bit As Integer
instring = "abc"
For i = 1 To Len(instring)
Onebyte = Asc(Mid(instring, i, 1))
For j = 0 To 7
bit = IIf((Onebyte And (2 ^ j)) > 0, 1, 0)
Debug.Print bit
Next j
Next i
 
Private Function ByteToBit(myByte As Byte) As String

'Bit == 32103210
' --------
'&HF0 == "11110000"
'&H0A == "00001010"

Dim k As Integer
Dim x As Integer

Dim valofbyte As Integer
Dim mybytetobit As String

valofbyte = Val(CStr(myByte))
mybytetobit = ""

For k = 0 To 7
x = 2 ^ k
If valofbyte And x Then
mybytetobit = "1" & mybytetobit
Else
mybytetobit = "0" & mybytetobit
End If

Next k

ByteToBit = mybytetobit

End Function

good luck

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
come on... get involved!
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 
See the bottom of thread222-654641 for some ideas on binary conversion
 
Thanks very much for your help. I will try these and let yo know what works.
 
Oops, that should have read "bottom of thread222-651652"
 
Hi! I am very interested in this topic(converting...).

Like Glasgow's example will convert a string into bit, but how could I convert it back to origianl form?

Thank you,
 
probably faster ways but here goes!

Private Function BitToDec(MyBit As String) As Integer

' 32103210
' --------
'"00001010" == 10
'"11010100" == 212

Dim k As Integer
Dim x As Integer
Dim mybittodec As Integer
Dim myLength As Integer

myLength = Len(MyBit)

For k = 1 To myLength
If Mid(MyBit, k, 1) = 1 Then
x = 2 ^ (myLength - k)
mybittodec = mybittodec + x
End If
Next k

BitToDec = mybittodec
End Function

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
come on... get involved!
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 
To ADoozer -

Thanks for the prompt respond, but I am trying to find ways to convert the 'bit' back into string value.

eg -
This is what I get after converting using the sample code above with string value of 'abc'
output = '100001100100011011000110'

So how could I use the output value and convert back to 'abc'? Thank you

cheers,
 
You could use code like this:

Dim sInput, sOutput As String
Dim iASCIICode As Integer
Dim i As Integer
sInput = "100001100100011011000110"
For i = 1 To Len(sInput) Step 8
iASCIICode = 0
iASCIICode = CInt(Mid(sInput, i, 1)) * 128
iASCIICode = iASCIICode + CInt(Mid(sInput, i + 1, 1)) * 64
iASCIICode = iASCIICode + CInt(Mid(sInput, i + 2, 1)) * 32
iASCIICode = iASCIICode + CInt(Mid(sInput, i + 3, 1)) * 16
iASCIICode = iASCIICode + CInt(Mid(sInput, i + 4, 1)) * 8
iASCIICode = iASCIICode + CInt(Mid(sInput, i + 5, 1)) * 4
iASCIICode = iASCIICode + CInt(Mid(sInput, i + 6, 1)) * 2
iASCIICode = iASCIICode + CInt(Mid(sInput, i + 7, 1))
sOutput = sOutput & Chr(iASCIICode)
Next i
 
To ICISYD -

I try your code but the output I got is '†FÆ'.... Please advice

Thank you,
 
Your binary string has the bits in each byte backwards - that is LSB first. Just change the multipliers round, or generate the string correctly in the first place.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
To johnwm -

What is 'LSB'? And could you please provide with the sample source you mention?

Thank you,
 
LSB - least significant bit.

The code in ICISYD's last post decodes the byte correctly if the bits are in the normal order, with the most significant bit first.

The string you are using :
<output = '100001100100011011000110'> is stored with least significant bit first.

In the code, just reverse the order of the powers of 2 that you multiply by!

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
To johnwm -

I still not quite get it(duhh....sorry), but I will test it out later with your advice and reply back here, thank you!

cheers,
 
Thanks to johnwm, ICISYD and all expert here! Isn't its nice to learn a new thing each day :)

cheers,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top