在java中从字节数组获取枚举

在java中从字节数组获取枚举,java,arrays,Java,Arrays,我有一个字节数组,其中包含按字节顺序排列的枚举值。我如何在java中迭代这样一个数组,并将每4个字节转换为一个枚举,然后将其放入这些枚举的数组中? 我有C++背景,所以我更熟悉指针算术。 谢谢大家! public enum MyEnum{ TAG1(0x0C00), TAG2(0x0C01), TAG3(0x0C02); private final int id; MyEnum(int id) { this.id = id; }

我有一个字节数组,其中包含按字节顺序排列的枚举值。我如何在java中迭代这样一个数组,并将每4个字节转换为一个枚举,然后将其放入这些枚举的数组中? 我有C++背景,所以我更熟悉指针算术。 谢谢大家!

public enum MyEnum{
    TAG1(0x0C00),
    TAG2(0x0C01),
    TAG3(0x0C02);

    private final int id;

    MyEnum(int id) {
        this.id = id;
    }

    public int getValue() {
        return id;
    }
}


byte[] data = getData(); //  returns  for example '01 0c 00 00 02 0c 00 00' which would I want to interpret as as {TAG2, TAG3}
// this returns a byte array with the enums in LE byte order,
// one after the other

MyEnum[] enums = new MyEnum[data.length / 4];
// now I would copy each enum from the byte array to the enum 
// array, but unsure how to do that in Java

像这样的怎么样

byte[] data = getData();
// this returns a byte array with the enums in LE byte order,
// one after the other

MyEnum[] enums = new MyEnum[data.length / 4];
// now I would copy each enum from the byte array to the enum 
// array, but unsure how to do that in Java

for (int i = 0; i < enums.length; i++) {
   // read whole enum
   int enumOrdinal = getOrdinalAt(data, i)
   MyEnum currentEnum = MyEnum.values()[enumOrdinal]
   enums[i] = currentEnum
}



private int getOrdinalAt(byte[] data, int enumIndex) {
   return data[enumIndex] || data[enumIndex + 1] || data[enumIndex + 2] || data[enumIndex + 3]
}
数据中读取字节后,只需使用
MyEnum.valueOf(readThing)
并获得相应的枚举

e、 g.TAG1表示0x0c00,TAG2表示0xc01,依此类推

Map<Integer, MyEnum> all = Arrays.stream(MyEnum.values())
    .collect(Collectors.toMap(MyEnum::getValue, e -> e));
byte[] data = { 0, 0xC, 0, 0, 1, 0xC, 0, 0, 2, 0xC, 0, 0 };
IntBuffer ib = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
MyEnum[] enums = new MyEnum[data.length / 4];
for (int i = 0; i < ib.capacity(); ++i)
    enums[i] = all.get(ib.get());
System.out.println(Arrays.toString(enums));
// -> [TAG1, TAG2, TAG3]
Map all=Arrays.stream(MyEnum.values())
.collect(Collectors.toMap(MyEnum::getValue,e->e));
字节[]数据={0,0xC,0,0,1,0xC,0,0,2,0xC,0,0};
IntBuffer ib=ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
MyEnum[]enums=新的MyEnum[data.length/4];
对于(int i=0;i[TAG1,TAG2,TAG3]

如何将MyEnum
转换为
字节[]
?使用它的名称、顺序、其他属性?您可以共享您的
MyEnum
类吗?如何将字节值存储为Enum?请注意,Java中的
Enum
是成熟的对象,而不是像在其他一些语言中那样美化
int
常量,因此,当您说“按字节顺序排列的Enum值”和“将每4个字节转换为一个Enum”时,您的意思并不十分清楚。如果您只将这些短语视为
int
(即它们的序数值),而不是从Java的角度来看,那么这些短语是有意义的。@JoachimSauer是的,这是有意义的。我添加了enum代码以进行澄清,现在我发现它与C enum完全不同。等等。。。您的
数据是包含枚举ID还是仅包含其索引?如果是枚举id,则需要枚举中的
值(…)
Map<Integer, MyEnum> all = Arrays.stream(MyEnum.values())
    .collect(Collectors.toMap(MyEnum::getValue, e -> e));
byte[] data = { 0, 0xC, 0, 0, 1, 0xC, 0, 0, 2, 0xC, 0, 0 };
IntBuffer ib = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
MyEnum[] enums = new MyEnum[data.length / 4];
for (int i = 0; i < ib.capacity(); ++i)
    enums[i] = all.get(ib.get());
System.out.println(Arrays.toString(enums));
// -> [TAG1, TAG2, TAG3]