I am relatively new to visual basic. I need to take two 8 bit byte values, join them to makea 16 bit binary value and then separate the 4 msb as a 4bit binary vale and the 12 lsb as a 12 bit binary value.
Thanks in advance for helping me out.
I am relatively new to visual basic. I need to take two 8 bit byte values, join them to makea 16 bit binary value and then separate the 4 msb as a 4bit binary vale and the 12 lsb as a 12 bit binary value.
Thanks in advance for helping me out.
Hi,
Post Some Sample Data here..
For What Input , What is the Output you want...?
Regards
Veena
Do you have
high byte__________low byte
l1l1l1l1l1l1l1l1l____l1l1l1l1l1l1l1l1l
Or you have two bytes like
high byte=255
low byte=255?
Those are example numbers.
Hi,
Try this
Dim byteA As Long
Dim byteB As Long
Dim sumBytes As Long
Dim fourBits As Long
Dim twelveBits As Long
byteA = 32 ' 00100000b = 32d
byteB = 64 ' 01000000b = 64d
' Adding the two bytes to get a Word
' sumBytes = 0010000001000000b = 2048b
sumBytes = byteA * 255 + byteB
' Take the four higher bits
' using this mask 1111000000000000b = 61440d
fourBits = sumBytes And 61440
' Take the next twelve bits
' using this mask 0000111111111111b = 4095d
twelveBits = sumBytes And 4095
MsgBox fourBits
MsgBox twelveBits
Just change the values of byteA and byteB.
Hope it helps
Hi Veena,Terapod, dmf1978
Thanks for your replies.
I see light in dmf1978's code. Using the code I am able to get the 12 bit integer as I need it, however, The 4 bits when separated are to be a nibble with values from 0 to 15.
Dim byteA As Byte
Dim byteB As Byte
byteA = 65 '01000001b = 65d
byteB = 32 '00100000b = 32d
( On joining the two bytes we get 0100000100100000 , and on masking the 4 bits (MSB) we get 000100100000 = 288d . Now I need to get the 4 bit nibble 0100 = 4d )
Best Regards,
praksk
Hi,
Well, I guess you have to shift right the variable fourBits 12 positions.
Try this (I am not really sure it will work, because I have not VB here)
Dim byteA As Long
Dim byteB As Long
Dim sumBytes As Long
Dim fourBits As Long
Dim twelveBits As Long
byteA = 32 ' 00100000b = 32d
byteB = 64 ' 01000000b = 64d
' Adding the two bytes to get a Word
' sumBytes = 0010000001000000b = 2048b
sumBytes = byteA * 255 + byteB
' Take the four higher bits
' using this mask 1111000000000000b = 61440d
fourBits = sumBytes And 61440
' Shift right twelve positions
fourBits = fourBits / 2 ^ 12
' Take the next twelve bits
' using this mask 0000111111111111b = 4095d
twelveBits = sumBytes And 4095
MsgBox fourBits
MsgBox twelveBits
Thats all nice but when i make code in vb i usually try to make it more universal so if i need it i can easy use it on other application and or if i need change output value i can do it much easier by changing input values
For example make conversion from numbering systems vb don't have binary numeric format as much as i know.
I just hope that you wont have too many combination for those binary numbers.
Just make simple function that will convert string witch have "binary value" and convert it to long value. that should be max 10 lines of code for any binary value you put in there.
Just copy and paste these function in your module.
Use it and you wont have to worry how much is 1100101 in dec? :-/
Function BinToDec(strBin As String) As Long
'If return value is -1 please check strBin value!
Dim lonBinLenght As Long
Dim lonDecMirror As Long
On Error GoTo Errorhandler
lonBinLenght = Len(strBin)
For i = 0 To lonBinLenght - 1
If (Mid(strBin, lonBinLenght - i, 1)) > 1 Then GoTo Errorhandler
lonDecMirror = lonDecMirror + (Mid(strBin, lonBinLenght - i, 1) * 2 ^ i)
Next
BinToDec = lonDecMirror
Exit Function
Errorhandler:
strBin = -1
End Function
Just make simple function that will convert string witch have "binary value" and convert it to long value. that should be max 10 lines of code for any binary value you put in there.
And it is nice too, but as he say, he don't have string binary values. All that I know is the existence of two bytes. I've coded taking account of these.
As you said... Have fun ;)
Then he can just use these function.
Its also simple.
Function DecToBin(lonDec As Long, lonNumberOfBits As Long) As String
Dim strBinMirror As String
Dim intBit As Integer
Dim lonDecMirror As Long
lonDecMirror = lonDec
Do While lonDecMirror > 0
intBit = lonDecMirror Mod 2
lonDecMirror = Fix(lonDecMirror / 2)
strBinMirror = intBit & strBinMirror
Loop
If Len(strBinMirror) < lonNumberOfBits Then
For i = 1 To lonNumberOfBits - Len(strBinMirror)
strBinMirror = "0" & strBinMirror
Next
End If
DecToBin = strBinMirror
End Function
Hope these two functions will help!
HI,
I see your point. But what he need is to separate some bits from a word. To do this, the easy way is to use masks. The mask thing is not an invention of mine.
Take a look here: http://en.wikipedia.org/wiki/Mask_(computing)
It is the more straightforward way to do that. And it uses less code too.
Sure, I am with you that he must code this inside a function, passing the original word, start_bit, end_bit, etc.
Regards
Hi Martin,
Your code for shifting the bits is working for me.
The crux of my problem was to extract the 4 MSB as a 4bit nibble ( values deciphered to be 0 to 15 ) and remaining LSB as a 12 bit integer, from the the 16 bit value.
Your entire code is doing the job well from joining the 2 byte values at the start to giving me the 4 and 12 bit values appropriately at the end.
BTW I had to make a change at line to get proper results ( *256 instead of *255 )
12. sumBytes = byteA * 256 + byteB
Thanks a ton.
Hi Teropod,
Your code was illustrative for the Dec2Bin and Bin2Dec conversion.
Thanks
BTW I had to make a change at line to get proper results ( *256 instead of *255 )
I see. I'll change it. Thanks.
If you think that your problem is solved, please, mark as solved this thread.
Regards
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.