| | |
Need to separate 16 bit binary to 4bit and 12 bit binary values
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Hi,
Try this
Just change the values of byteA and byteB.
Hope it helps
Try this
VB Syntax (Toggle Plain Text)
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
Last edited by dmf1978; Jul 25th, 2008 at 10:30 am.
-- Martín
•
•
Join Date: Jul 2008
Posts: 4
Reputation:
Solved Threads: 0
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
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)
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)
VB Syntax (Toggle Plain Text)
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
Last edited by dmf1978; Jul 25th, 2008 at 1:26 pm.
-- Martín
•
•
Join Date: Jul 2008
Posts: 36
Reputation:
Solved Threads: 6
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?
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?
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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.
As you said... Have fun
-- Martín
•
•
Join Date: Jul 2008
Posts: 36
Reputation:
Solved Threads: 6
Then he can just use these function.
Its also simple.
Hope these two functions will help!
Its also simple.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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
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
Last edited by dmf1978; Jul 25th, 2008 at 7:30 pm.
-- Martín
![]() |
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: Bind ListBox to ADODB.Recordset
- Next Thread: Need your opinion:
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age append application basic beginner birth bmp calculator cd cells.find click client code college column component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report retrieve save search sendbyte sites sort sql sql2008 sqlserver subroutine table tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





