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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

binary parsing 3

Status
Not open for further replies.

deulyd

Programmer
Oct 17, 2001
106
CA
Hi, I have a property of a control that contains a decimal value for allowing Rights on something...

Example :

4 = Can Open
8 = Can Save
16 = Can Modify
32 = Can Copy
64 = Can Paste
128 = Can Rotate
etc...

So for example if I can Open, Modify and Paste the value of the property would be 84.

Does anybody has a routine to parse those rights or have a clue, I dont know where to start.

Thanks

Daniel
 
This is a perfect use of the binary AND operator.

Here's some code to get you started. Please study this. If you have any question about HOW it works, let me know and I will explain further.

Code:
Private Sub Form_Load()
    
    MsgBox "Allow Open: " & AllowOpen(84)
    MsgBox "Allow Save: " & AllowSave(84)
End Sub

Public Function AllowOpen(ByVal DecimalValue As Integer) As Boolean
    
    AllowOpen = (DecimalValue And 4) = 4
    
End Function

Public Function AllowSave(ByVal DecimalValue As Integer) As Boolean
    
    AllowSave = (DecimalValue And 8) = 8
    
End Function

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
theres lots of ways to do it, but it would depend on the rest of your code to get the most efficient usage.

a simple method would be

Code:
Const CANOPEN As Integer = 4
Const CANSAVE As Integer = 8
Const CANMODIFY As Integer = 16
Const CANCOPY As Integer = 32
Const CANPASTE As Integer = 64
Const CANROTATE As Integer = 128

Public Sub DoFlags(inpLong As Long)

    If inpLong And CANOPEN Then MsgBox "Can Open", vbOKOnly
    If inpLong And CANSAVE Then MsgBox "Can Save", vbOKOnly
    If inpLong And CANMODIFY Then MsgBox "Can Modify", vbOKOnly
    If inpLong And CANCOPY Then MsgBox "Can Copy", vbOKOnly
    If inpLong And CANPASTE Then MsgBox "Can Paste", vbOKOnly
    If inpLong And CANROTATE Then MsgBox "Can Rotate", vbOKOnly
    
End Sub

Private Sub Form_Load()
    DoFlags 84
End Sub

good luck

If somethings hard to do, its not worth doing - Homer Simpson
 
drat... too slow again :(

If somethings hard to do, its not worth doing - Homer Simpson
 
Don't forget you must not ADD the values to get a new property value - you must use OR. Using ADoozer's constants:
Code:
Const CANOPEN As Integer = 4
'to set CANOPEN
myFlags = myFlags OR CANOPEN
If you use:
Code:
myFlags = myFlags + CANOPEN
when CANOPEN is already true you will set it to False and also toggle higher bits

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
Thanks you all guys, great tips! :D

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top