Java short[]与byte[]互转

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/DeMonliuhui/article/details/95320092
  /**
     * byte数组转short数组
     *
     * @param src
     * @return     
     */
    private short[] toShortArray(byte[] src) {

        int count = src.length >> 1;
        short[] dest = new short[count];
        for (int i = 0; i < count; i++) {
            dest[i] = (short) ((src[i * 2] & 0xff) | ((src[2 * i + 1] & 0xff) << 8));
        }
        return dest;
    }

    /**
     * short数组转byte数组
     *
     * @param src
     * @return     
     */
    private byte[] toByteArray(short[] src) {
        int count = src.length;
        byte[] dest = new byte[count << 1];
        for (int i = 0; i < count; i++) {
            dest[i * 2] = (byte) (src[i]);
            dest[i * 2 + 1] = (byte) (src[i] >> 8);
        }
        return dest;
    }

参考

https://blog.csdn.net/vk5176891/article/details/52785984

猜你喜欢

转载自blog.csdn.net/DeMonliuhui/article/details/95320092