I'm sure this is easy...but... [Decimal -> Hex and Bitwise] 
Author Message
 I'm sure this is easy...but... [Decimal -> Hex and Bitwise]

Here's what I'm trying to do without a clue where to start.
I want to store settings in a binary field in a database [that's the easy
part for me]

let's say i had 3 options, Add, Edit, Approve, and Delete and these were
different bits in a binary respectively.

0000 No options
1000 Add only
1001 Add and Delete
0100 Edit only
etc, etc...

First, how do i convert this binary into a Hex (&H) to set to constants in
VB?

Second, how do i do comparisons to see if a bit is set?
I'll assume something using the And operator..

Third, is this all worth it?
I could just as easily use a text field to do the same..
A - Add
AD Add and Delete

etc, etc...

Thanks for the wisdom,
-=Chris



Fri, 15 Feb 2002 03:00:00 GMT  
 I'm sure this is easy...but... [Decimal -> Hex and Bitwise]
You may be better off working with the decimal values instead of binary
values.  I have had a lot of success in the past with using decimal values:

0 = &H0 = 0000 No options
8 = &H8 = 1000 Add only
9 = &H9 = 1001 Add and Delete
4 = &H4 = 0100 Edit only
1 = &H1 = 0001 Delete

With this method you can convert to hexidecimal using the Hex function.  You
can then use the logical operators to do your comparisons.  Something like:

Const USER_ADD = &H8
Const USER_NONE = &H0
Const USER_ADDDELETE = &H9
Const USER_EDIT = &H4
Const USER_DELETE = &H1

If User.Permissions And USER_EDIT Then
    ' Do something
End IF

--
Matthew Arnheiter
Flash Creative Management
http://www.flashcreative.com



Quote:
> Here's what I'm trying to do without a clue where to start.
> I want to store settings in a binary field in a database [that's the easy
> part for me]

> let's say i had 3 options, Add, Edit, Approve, and Delete and these were
> different bits in a binary respectively.

> 0000 No options
> 1000 Add only
> 1001 Add and Delete
> 0100 Edit only
> etc, etc...

> First, how do i convert this binary into a Hex (&H) to set to constants in
> VB?

> Second, how do i do comparisons to see if a bit is set?
> I'll assume something using the And operator..

> Third, is this all worth it?
> I could just as easily use a text field to do the same..
> A - Add
> AD Add and Delete

> etc, etc...

> Thanks for the wisdom,
> -=Chris



Fri, 15 Feb 2002 03:00:00 GMT  
 I'm sure this is easy...but... [Decimal -> Hex and Bitwise]


Quote:
>Here's what I'm trying to do without a clue where to start.
>I want to store settings in a binary field in a database [that's the easy
>part for me]

>let's say i had 3 options, Add, Edit, Approve, and Delete and these were
>different bits in a binary respectively.

>0000 No options
>1000 Add only
>1001 Add and Delete
>0100 Edit only
>etc, etc...

>First, how do i convert this binary into a Hex (&H) to set to constants in
>VB?

Why convert to hex? - use (based on your binary) or a user-defined Type:-

    CONST OPT_NONE = 2^0
    CONST OPT_ADD = 2^4
    CONST OPT_EDIT = 2^3 etc

Quote:

>Second, how do i do comparisons to see if a bit is set?
>I'll assume something using the And operator..

IF (myVariable And OPT_NONE)<>0 Then ' "None" code...

IF (myvariable And OPT_ADD)<>0 Then ' "Add" code...

Quote:

>Third, is this all worth it?
>I could just as easily use a text field to do the same..
>A - Add
>AD Add and Delete

>etc, etc...

Bit-wise variables do reduce storage - in a Long you'll have 32 options in 4
bytes compared to 32 characters in 32 bytes (ASCII) or 64 bytes (UNICODE).
Speed will also be quicker as opposed to checking Mid$ of your option
string. Just remember to use readable constants.
Quote:

>Thanks for the wisdom,
>-=Chris



Sat, 16 Feb 2002 03:00:00 GMT  
 I'm sure this is easy...but... [Decimal -> Hex and Bitwise]
ok, now for my next stupid question...
How do i put these together into a variable?

Permissions = CONST OPT_NONE + CONST OPT_ADD

?

Thanks,
-=Chris


Quote:



> >Here's what I'm trying to do without a clue where to start.
> >I want to store settings in a binary field in a database [that's the easy
> >part for me]

> >let's say i had 3 options, Add, Edit, Approve, and Delete and these were
> >different bits in a binary respectively.

> >0000 No options
> >1000 Add only
> >1001 Add and Delete
> >0100 Edit only
> >etc, etc...

> >First, how do i convert this binary into a Hex (&H) to set to constants
in
> >VB?

> Why convert to hex? - use (based on your binary) or a user-defined Type:-

>     CONST OPT_NONE = 2^0
>     CONST OPT_ADD = 2^4
>     CONST OPT_EDIT = 2^3 etc

> >Second, how do i do comparisons to see if a bit is set?
> >I'll assume something using the And operator..

> IF (myVariable And OPT_NONE)<>0 Then ' "None" code...

> IF (myvariable And OPT_ADD)<>0 Then ' "Add" code...

> >Third, is this all worth it?
> >I could just as easily use a text field to do the same..
> >A - Add
> >AD Add and Delete

> >etc, etc...

> Bit-wise variables do reduce storage - in a Long you'll have 32 options in
4
> bytes compared to 32 characters in 32 bytes (ASCII) or 64 bytes (UNICODE).
> Speed will also be quicker as opposed to checking Mid$ of your option
> string. Just remember to use readable constants.

> >Thanks for the wisdom,
> >-=Chris



Sat, 16 Feb 2002 03:00:00 GMT  
 I'm sure this is easy...but... [Decimal -> Hex and Bitwise]

You can add the options using + but this is prone to error.
If you add the same option twice you get the next option instead.
If the options are 0, 1, 2, 4, 8, etc
    0 + 1 = 1
    0 + 1 + 4 = 5
but
    1 + 1 = 2
A better way is to use the Or operator.
    0 Or 1 = 1
    0 Or 1 Or 4 = 5
and
    1 Or 1 = 1

You test them using the And operator
    If (intOptions And 4) Then
    ...
This works because (intOptions And 4) is always 0 unless you added/or-ed 4 into it in the first place. In this case it would give 4 as the result and anything that isn't false (zero) is considered to be true.

Regards,
Simon Jones
MillStream Designs Ltd
Independent IT Consultants


  ok, now for my next stupid question...
  How do i put these together into a variable?
  Permissions = CONST OPT_NONE + CONST OPT_ADD
  ?



Sat, 16 Feb 2002 03:00:00 GMT  
 I'm sure this is easy...but... [Decimal -> Hex and Bitwise]
The problem with plus is that if the constants are not disjoint, then
adding them together will give rise to side effects. Using Matthew's
constants...

Const USER_ADD = &H8
Const USER_NONE = &H0
Const USER_ADDDELETE = &H9
Const USER_EDIT = &H4
Const USER_DELETE = &H1

If you set Permissions = USER_ADD + USER_ADDDELETE the result is &H11
which effectively evaluates to USER_DELETE, probably not what was
intended. The solution is to use OR as the operator, that way if a bit is
set in more than one of the constants it's effect will only be felt once.

Regards

Paul Hatcher, MCSD
Principal Consultant
Graduate Associates Ltd



Sun, 17 Feb 2002 03:00:00 GMT  
 I'm sure this is easy...but... [Decimal -> Hex and Bitwise]
Example please?
nothing i try works using Or

Permissions = USER_ADD Or USER_EDIT
USER_EDIT  Or USER_ADD  = Permissions

?

Thanks,
-=Chris



Quote:
> The problem with plus is that if the constants are not disjoint, then
> adding them together will give rise to side effects. Using Matthew's
> constants...

> Const USER_ADD = &H8
> Const USER_NONE = &H0
> Const USER_ADDDELETE = &H9
> Const USER_EDIT = &H4
> Const USER_DELETE = &H1

> If you set Permissions = USER_ADD + USER_ADDDELETE the result is &H11
> which effectively evaluates to USER_DELETE, probably not what was
> intended. The solution is to use OR as the operator, that way if a bit is
> set in more than one of the constants it's effect will only be felt once.

> Regards

> Paul Hatcher, MCSD
> Principal Consultant
> Graduate Associates Ltd



Mon, 18 Feb 2002 03:00:00 GMT  
 I'm sure this is easy...but... [Decimal -> Hex and Bitwise]
Here's some actual code I used:

Public User_Rights As Long

'Constants for the rights
Private Const FLAG_DELETE_TRANSMITTAL = &H1 'Can delete transmittals
Private Const FLAG_CREATE_TRANSMITTAL = &H2 'Can create a new transmittal
Private Const FLAG_MODIFY_TRANSMITTAL = &H8 'Can do most anything to a
transmittal but delete it
Private Const FLAG_ADD_DOCUMENTS = &H10 'Allow manual document entry
Private Const FLAG_MODIFY_ADDRESS = &H20  'Can modify the address
information
Private Const FLAG_EDIT_ROLES = &H40
Private Const FLAG_EDIT_TRANSMITTAL = &H80
Private Const FLAG_EDIT_LISTS = &H100

'The 4 types of roles that a person can fall under
Private Const ROLE_GUEST = 0  'Can't do anything
Private Const ROLE_STANDARD = FLAG_MODIFY_TRANSMITTAL Or FLAG_ADD_DOCUMENTS
Or FLAG_MODIFY_ADDRESS Or FLAG_CREATE_TRANSMITTAL Or FLAG_EDIT_ROLES
Private Const ROLE_ADMIN = ROLE_STANDARD Or FLAG_DELETE_TRANSMITTAL Or
FLAG_EDIT_TRANSMITTAL Or FLAG_EDIT_LISTS
Private Const ROLE_SYS_ADMIN = ROLE_ADMIN

Private Sub GetUserRights()
  Select Case GetUserRightsLevel()  'Gets a numeric level from the database
    Case RIGHTS_STANDARD
      User_Rights = ROLE_STANDARD
    Case RIGHTS_ADMIN
      User_Rights = ROLE_ADMIN
    Case RIGHTS_SYS_ADMIN
      User_Rights = ROLE_SYS_ADMIN
    Case Else
      'Set the default rights
      User_Rights = ROLE_GUEST
  End Select
End Sub

Heres some code to enable/disable some toolbar buttons depending on their
rights.  The Ands will return zero if they don't have that bit set,
otherwise they return a non-zero value (specifically, the value of the
flag).

  With mdiMain.Toolbar3
    .Buttons(BUTTON_SAVE).Enabled = (User_Rights And
FLAG_MODIFY_TRANSMITTAL)
    .Buttons(BUTTON_ADDRESS).Enabled = (User_Rights And FLAG_MODIFY_ADDRESS)
    .Buttons(BUTTON_NEW).Enabled = (User_Rights And FLAG_CREATE_TRANSMITTAL)
    .Buttons(BUTTON_CANCEL).Enabled = (User_Rights And
FLAG_MODIFY_TRANSMITTAL)
   End With



Tue, 19 Feb 2002 03:00:00 GMT  
 I'm sure this is easy...but... [Decimal -> Hex and Bitwise]
Thanks!!!!
That clears things up perfectly!

-=Chris


Quote:
> Here's some actual code I used:

> Public User_Rights As Long

> 'Constants for the rights
> Private Const FLAG_DELETE_TRANSMITTAL = &H1 'Can delete transmittals
> Private Const FLAG_CREATE_TRANSMITTAL = &H2 'Can create a new transmittal
> Private Const FLAG_MODIFY_TRANSMITTAL = &H8 'Can do most anything to a
> transmittal but delete it
> Private Const FLAG_ADD_DOCUMENTS = &H10 'Allow manual document entry
> Private Const FLAG_MODIFY_ADDRESS = &H20  'Can modify the address
> information
> Private Const FLAG_EDIT_ROLES = &H40
> Private Const FLAG_EDIT_TRANSMITTAL = &H80
> Private Const FLAG_EDIT_LISTS = &H100

> 'The 4 types of roles that a person can fall under
> Private Const ROLE_GUEST = 0  'Can't do anything
> Private Const ROLE_STANDARD = FLAG_MODIFY_TRANSMITTAL Or
FLAG_ADD_DOCUMENTS
> Or FLAG_MODIFY_ADDRESS Or FLAG_CREATE_TRANSMITTAL Or FLAG_EDIT_ROLES
> Private Const ROLE_ADMIN = ROLE_STANDARD Or FLAG_DELETE_TRANSMITTAL Or
> FLAG_EDIT_TRANSMITTAL Or FLAG_EDIT_LISTS
> Private Const ROLE_SYS_ADMIN = ROLE_ADMIN

> Private Sub GetUserRights()
>   Select Case GetUserRightsLevel()  'Gets a numeric level from the
database
>     Case RIGHTS_STANDARD
>       User_Rights = ROLE_STANDARD
>     Case RIGHTS_ADMIN
>       User_Rights = ROLE_ADMIN
>     Case RIGHTS_SYS_ADMIN
>       User_Rights = ROLE_SYS_ADMIN
>     Case Else
>       'Set the default rights
>       User_Rights = ROLE_GUEST
>   End Select
> End Sub

> Heres some code to enable/disable some toolbar buttons depending on their
> rights.  The Ands will return zero if they don't have that bit set,
> otherwise they return a non-zero value (specifically, the value of the
> flag).

>   With mdiMain.Toolbar3
>     .Buttons(BUTTON_SAVE).Enabled = (User_Rights And
> FLAG_MODIFY_TRANSMITTAL)
>     .Buttons(BUTTON_ADDRESS).Enabled = (User_Rights And

FLAG_MODIFY_ADDRESS)
Quote:
>     .Buttons(BUTTON_NEW).Enabled = (User_Rights And

FLAG_CREATE_TRANSMITTAL)

- Show quoted text -

Quote:
>     .Buttons(BUTTON_CANCEL).Enabled = (User_Rights And
> FLAG_MODIFY_TRANSMITTAL)
>    End With



Sat, 23 Feb 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. >>= bitwise operations >>

2. Hex <----> Decimal conversions

3. Hex => Decimal

4. Hex->Decimal

5. I'm sure that this is an easy question

6. Really easy I'm sure...

7. A very easy question - I'm sure

8. A very easy question - I'm sure

9. Easy question I'm sure - never written vbscript before

10. hex string -> hex number

11. hex-color's -----> vbcolors

12. Help please with a simple problem I am sure for an experienced programmer

 

 
Powered by phpBB® Forum Software