I have to read/write an XML file that stores its data as bit masked values.
e.g.
<panel>
<options arming="21" opt_b="51" opt_c="6" />
</panel>
in this case, arming has the value 21, but this corresponds to the following string of bytes:
10101
and those bytes correspond to options 0,2,4 in the following list:
- Bit 0: Forced arm
- Bit 1: Final door
- Bit 2: Exit fault
- Bit 3: Inhibit tamper
- Bit 4: Display
- Bit 8: Rearm
- Bit 9: Line
- Bit 10: Extend block
What’s the best way for me to
- convert this from the decimal value of
21to an object model that represents the options? - use that object to display these as options for the user to select and deselect?
Declare an enum thus:
Note my use of powers of two in the enum. These allow an enum to represent discrete values using binary bit positions (see Why do enum permissions often have 0, 1, 2, 4 values?)
then parse your value into an integer and cast that to a type of ArmingFlags (note, untested code ahead but should work):
There are lots of posts on how to use an enum in a WPF combobox or radio buttons, for instance:
Databinding an enum property to a ComboBox in WPF