将字节数组大小减小到其原始大小的一半



我有一个长度为 14 的字节数组。我可以将第一个两个元素合并为一个,依此类推,使其大小为 7 吗?即<{730C5454000160}>应该看起来像

<{73,0C,54,54,00,01,60}>.

或如果没有,我有一个字符串"730C5454000160",我需要它作为字节数组,例如

<{73,0C,54,54,00,01,60}>.

请有人帮我,谢谢。

让 JDK 帮助你:

byte[] bytes = new byte[7];
System.arraycopy( ByteBuffer.allocate(8).putLong( Long.parseLong( s, 16 ) ).array(), 1, bytes, 0, 7);

下面是一些测试代码:

public static void main( String[] args ) {
    String s = "730C5454000160";
    byte[] bytes = new byte[7];
    System.arraycopy( ByteBuffer.allocate(8).putLong( Long.parseLong( s, 16 ) ).array(), 1, bytes, 0, 7);
    System.out.println( Arrays.toString(bytes ));
}

输出:

[115, 12, 84, 84, 0, 1, 96]

最新更新