将字节中的位解析为枚举

时间:2013-06-20 08:42:39

标签: c# enums binary

我正在研究一个解析我从家庭自动化模块获得的二进制数据的DLL。

但是我需要一些关于我的代码的建议 所以我得到一个带有一些字节的消息,在这种情况下每个位都表示一定的条件。

enter image description here

在我目前的每个条件都是enum的代码中,我将枚举放在一个数组中并检查相应的位是否已设置。

private void ParseZoneConditionFlag1(int Flag1)  // Flag1 = Hex represenation of byte
{
    Zone_Status_ZoneConditionFlagEnum[] FlagArray = new Zone_Status_ZoneConditionFlagEnum[8];
    FlagArray[0] = Zone_Status_ZoneConditionFlagEnum.Faulted;
    FlagArray[1] = Zone_Status_ZoneConditionFlagEnum.Tampered;
    FlagArray[2] = Zone_Status_ZoneConditionFlagEnum.Trouble;
    FlagArray[3] = Zone_Status_ZoneConditionFlagEnum.Bypassed;
    FlagArray[4] = Zone_Status_ZoneConditionFlagEnum.Inhibited;
    FlagArray[5] = Zone_Status_ZoneConditionFlagEnum.Low_Battery;
    FlagArray[6] = Zone_Status_ZoneConditionFlagEnum.Loss_Supervision;
    FlagArray[7] = Zone_Status_ZoneConditionFlagEnum.Reserved;

    base.CheckBitsSet(FlagArray, Flag1, ZoneConditionFlags_List);
 }

 private void ParseZoneConditionFlag2(int Flag2)
 {
    Zone_Status_ZoneConditionFlagEnum[] FlagArray = new Zone_Status_ZoneConditionFlagEnum[8];
    FlagArray[0] = Zone_Status_ZoneConditionFlagEnum.Alarm_Memory;
    FlagArray[1] = Zone_Status_ZoneConditionFlagEnum.Bypass_Memory;
    FlagArray[2] = Zone_Status_ZoneConditionFlagEnum.Reserved;
    FlagArray[3] = Zone_Status_ZoneConditionFlagEnum.Reserved;
    FlagArray[4] = Zone_Status_ZoneConditionFlagEnum.Reserved;
    FlagArray[5] = Zone_Status_ZoneConditionFlagEnum.Reserved;
    FlagArray[6] = Zone_Status_ZoneConditionFlagEnum.Reserved;
    FlagArray[7] = Zone_Status_ZoneConditionFlagEnum.Reserved;

    base.CheckBitsSet(FlagArray, Flag2, ZoneConditionFlags_List);
 }

方法是我检查实际位

 protected void CheckBitsSet<T>(T[] ConstantArray, int HexValue, List<T> DestinationList)
 {
     byte b = (byte) HexValue;

     for (int i = 0; i < Mask.Length; i++)
     {
        if(IsBitSet(b, i))
        {
           DestinationList.Add(ConstantArray[i]);
        }
     }
 }

 public bool IsBitSet(byte b, int pos)
 {
    return (b & (1 << pos)) != 0;
 }

这有效,但我想知道是否有更清洁的方法来做到这一点 使用清洁器,我的意思是无需每次都向阵列添加正确的枚举。

2 个答案:

答案 0 :(得分:10)

如何:

[Flags]
enum MyFlags : short
{
    None = 0,
    Faulted = 1 << 0,
    Tampered = 1 << 1,
    Trouble = 1 << 2,
    Bypassed = 1 << 3,
    Inhibited = 1 << 4,
    LowBattery = 1 << 5,
    LossOfSupervision = 1 << 6,
    AlarmMemory = 1 << 8,
    BypassMemory = 1 << 9
}
static bool IsSet(MyFlags value, MyFlags flag)
{
    return ((value & flag) == flag);
}

并将值读取为2字节值(short,注意字节顺序),然​​后转换为MyFlags

要检查任何标志,只需:

MyFlags value = ...
bool isAlarmMemory = IsSet(value, MyFlags.AlarmMemory);

当你谈论复合标志时,它会变得棘手,即

bool memoryProblem = IsSet(value, MyFlags.AlarmMemory | MyFlags.BypassMemory);

因为你需要弄清楚你的意思是“是否设置了这些标志?” vs“所有这些标志都设置了吗?”

归结为测试;

return ((value & flag) == flag); // means "are all set"
return ((value & flag) != 0); // means "is any set"

阅读:

// this is just some garbage that I'm pretending is a message from
// your module; I'm assuming the byte numbers in the image are
// zero-based, so the two that we want are: \/\/\/ (the 6,3)
byte[] data = { 12, 63, 113, 0, 13, 123, 14, 6, 3, 14, 15 };

// and I'm assuming "byte 7" and "byte 8" (image) are zero-based;
// MyFlags uses byte 7 *first*, so it is little-endian; we can get that
// via:
short flagsRaw = (short)(data[7] | (data[8] << 8));
MyFlags flags = (MyFlags)flagsRaw;
// flags has value Tampered | Trouble | AlarmMemory | BypassMemory,
// which is what we expect for {6,3}

答案 1 :(得分:1)

使用此:

[Flags]
public enum MyEnum
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 4,
    Value5 = 8
}

(...)

void Func(int flag)
{
    MyEnum @enum = (MyEnum)flag;

    // Testing, whether a flag is set

    if ((@enum & MyEnum.Value1) != 0) // sth
}