使用十六进制数初始化无符号字节数组

发布于 2025-01-04 19:18:28 字数 1216 浏览 2 评论 0原文

我知道Java中缺少无符号字节 那么如何使用 0 到 255(十六进制)的整数初始化字节数组

    final byte assoc_resp_msg_int[] = new byte[] {
            0xe3, 0x00, //APDU CHOICE Type(AareApdu)
            0x00, 0x2c, //CHOICE.length = 44
            0x00, 0x00, //result=accept
            0x50, 0x79, //data-proto-id = 20601
            0x00, 0x26, //data-proto-info length = 38
            0x80, 0x00, 0x00, 0x00, //protocolVersion
            0x80, 0x00, //encoding rules = MDER
            0x80, 0x00, 0x00, 0x00, //nomenclatureVersion
            0x00, 0x00, 0x00, 0x00, //functionalUnits, normal Association
            0x80, 0x00, 0x00, 0x00, //systemType = sys-type-manager
            0x00, 0x08, //system-id length = 8 and value (manufacturer- and device- specific) 
            0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
            0x00, 0x00, //Manager's response to config-id is always 0
            0x00, 0x00, //Manager's response to data-req-mode-flags is always 0
            0x00, 0x00, //data-req-init-agent-count and data-req-init-manager-count are always 0
            0x00, 0x00, 0x00, 0x00, //optionList.count = 0 | optionList.length = 0
    };

I know that unsigned byte is missing in Java
Then how can I initialize the byte array using integer from 0 to 255 (in hex)?

    final byte assoc_resp_msg_int[] = new byte[] {
            0xe3, 0x00, //APDU CHOICE Type(AareApdu)
            0x00, 0x2c, //CHOICE.length = 44
            0x00, 0x00, //result=accept
            0x50, 0x79, //data-proto-id = 20601
            0x00, 0x26, //data-proto-info length = 38
            0x80, 0x00, 0x00, 0x00, //protocolVersion
            0x80, 0x00, //encoding rules = MDER
            0x80, 0x00, 0x00, 0x00, //nomenclatureVersion
            0x00, 0x00, 0x00, 0x00, //functionalUnits, normal Association
            0x80, 0x00, 0x00, 0x00, //systemType = sys-type-manager
            0x00, 0x08, //system-id length = 8 and value (manufacturer- and device- specific) 
            0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
            0x00, 0x00, //Manager's response to config-id is always 0
            0x00, 0x00, //Manager's response to data-req-mode-flags is always 0
            0x00, 0x00, //data-req-init-agent-count and data-req-init-manager-count are always 0
            0x00, 0x00, 0x00, 0x00, //optionList.count = 0 | optionList.length = 0
    };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2025-01-11 19:18:28

您必须像这样以字节形式存储 0x80

final byte assoc_resp_msg_int[] = new byte[] {
        (byte)0xe3, 0x00, //APDU CHOICE Type(AareApdu)
        0x00, 0x2c, //CHOICE.length = 44
        0x00, 0x00, //result=accept
        0x50, 0x79, //data-proto-id = 20601
        0x00, 0x26, 
        (byte)0x80,
...
}
System.out.println(assoc_resp_msg_int[10]&0xFF);
//128

You have to store 0x80 in byte like this :

final byte assoc_resp_msg_int[] = new byte[] {
        (byte)0xe3, 0x00, //APDU CHOICE Type(AareApdu)
        0x00, 0x2c, //CHOICE.length = 44
        0x00, 0x00, //result=accept
        0x50, 0x79, //data-proto-id = 20601
        0x00, 0x26, 
        (byte)0x80,
...
}
System.out.println(assoc_resp_msg_int[10]&0xFF);
//128
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文