我如何初始化Java的字节数组? [英] How do I initialize a byte array in Java?

查看:153
本文介绍了我如何初始化Java的字节数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在byte数组形式的一些常量(的UUID)存储在Java中,我想知道什么是最好的方式来初始化这些静态数组会。这就是我现在怎么做,但我觉得必须有一个更好的办法。

 私有静态最后字节[] = CDRIVES新的字节[] {(字节)0xe0的,0x4f(字节)0xd0,
    为0x20,(字节)0xea,0x3a,0×69,0×10,(字节)0xa2(字节)0xd8,0x08的,为0x00,0x2B访问,
    为0x30,为0x30,(字节)0x9d};
私有静态最后一个字节[] = CMYDOCS新的字节[] {(字节)0xba,(字节)0x8a,符进行,
    ×45,0x25(字节)写入0xAD,(字节)0xd0,为0x11(字节)0x98在全局(字节)0xa8,0x08的,0x00时,
    0x36数据,0x1b,为0x11,0×03};
私有静态最后一个字节[] = IEFRAME新的字节[] {(字节)0x80的,0x53,为0x1C,
    (字节)87H的,(字节)0XA0,0x42后,0×69,0×10,(字节)0xa2(字节)0xea,0x08的,
    为0x00,0x2B访问,为0x30,为0x30,(字节)0x9d};
...
等等

有什么我可以使用,可能会降低效率,但会看清洁?
例如:

 私有静态最后字节[] = CDRIVES
    新的字节[] {0xe04fd020ea3a6910a2d808002b30309d};


解决方案

使用功能的六字符串转换为字节[] ,你可以做

 字节[] = CDRIVES hexStringToByteArray(e04fd020ea3a6910a2d808002b30309d);

我建议你使用由戴维·L的<一个函数定义href=\"http://stackoverflow.com/questions/140131/convert-a-string-re$p$psentation-of-a-hex-dump-to-a-byte-array-using-java\">Convert一个字符串,再一个十六进制转储的presentation使用Java的字节数组?

我在这里插入它最大的可读性:

 公共静态的byte [] hexStringToByteArray(String s)将{
    INT LEN = s.length();
    字节[]数据=新字节[len个/ 2];
    的for(int i = 0; I&LT; LEN;我+ = 2){
        数据[I / 2] =(字节)((Character.digit将(s.charAt(ⅰ),16)所述; 4;)
                             + Character.digit将(s.charAt第(i + 1),16));
    }
    返回的数据;
}

如果你让CDRIVES 静态最后,业绩下滑是不相关的。

I have to store some constant values (UUIDs) in byte array form in java, and I'm wondering what the best way to initialize those static arrays would be. This is how I'm currently doing it, but I feel like there must be a better way.

private static final byte[] CDRIVES = new byte[] { (byte)0xe0, 0x4f, (byte)0xd0,
    0x20, (byte)0xea, 0x3a, 0x69, 0x10, (byte)0xa2, (byte)0xd8, 0x08, 0x00, 0x2b,
    0x30, 0x30, (byte)0x9d };
private static final byte[] CMYDOCS = new byte[] { (byte)0xba, (byte)0x8a, 0x0d,
    0x45, 0x25, (byte)0xad, (byte)0xd0, 0x11, (byte)0x98, (byte)0xa8, 0x08, 0x00,
    0x36, 0x1b, 0x11, 0x03 };
private static final byte[] IEFRAME = new byte[] { (byte)0x80, 0x53, 0x1c,
    (byte)0x87, (byte)0xa0, 0x42, 0x69, 0x10, (byte)0xa2, (byte)0xea, 0x08,
    0x00, 0x2b, 0x30, 0x30, (byte)0x9d };
...
and so on

Is there anything I could use that may be less efficient, but would look cleaner? for example:

private static final byte[] CDRIVES =
    new byte[] { "0xe04fd020ea3a6910a2d808002b30309d" };

解决方案

Using a function converting an hexa string to byte[], you could do

byte[] CDRIVES = hexStringToByteArray("e04fd020ea3a6910a2d808002b30309d");

I'd suggest you use the function defined by Dave L in Convert a string representation of a hex dump to a byte array using Java?

I insert it here for maximum readability :

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

If you let CDRIVES static and final, the performance drop is irrelevant.

这篇关于我如何初始化Java的字节数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆