JAVA 整数网络字节转换(高低位互换)

2021年7月31日更新


/**
* 丑陋的java
*/
public class number {
   
    //region 数字与字节转换
    public static final byte[] bswap(short x) {
        return new byte[]{
                (byte) (x & 0x00ff),
                (byte) ((x >> 8) & 0x00ff)
        };
    }

    public static final byte[] bswap(int x) {
        return new byte[]{
                (byte) (x & 0x000000ff),
                (byte) ((x >> 8) & 0x000000ff),
                (byte) ((x >> 16) & 0x000000ff),
                (byte) ((x >> 24) & 0x000000ff)
        };
    }

    public static final byte[] bswap(long x) {
        return new byte[]{
                (byte) (x & 0x00000000000000ffL),
                (byte) ((x >> 8) & 0x00000000000000ffL),
                (byte) ((x >> 16) & 0x00000000000000ffL),
                (byte) ((x >> 24) & 0x00000000000000ffL),
                (byte) ((x >> 32) & 0x00000000000000ffL),
                (byte) ((x >> 40) & 0x00000000000000ffL),
                (byte) ((x >> 48) & 0x00000000000000ffL),
                (byte) ((x >> 56) & 0x00000000000000ffL)
        };
    }

    //65279
    public static int bytesToInt16(byte[] data) {
        /**
         * 丑陋的java
         */
        return (
                ((data[0] & 0x000000ff)) |
                        (((data[1] & 0x000000ff) << 8))
        );
    }

    public static int bytesToInt32(byte[] data) {
        return (
                ((data[0] & 0x000000ff)) |
                        ((((data[1] & 0x000000ff) << 8))) |
                        ((((data[2] & 0x000000ff) << 16))) |
                        ((((data[3] & 0x000000ff) << 24)))
        );
    }

    public static long bytesToInt64(byte[] data) {
        return (
                ((long) ((data[0]) & 0x00000000000000ffL)) |
                        (((long) (((data[1]) & 0x00000000000000ffL))) << 8) |
                        (((long) (((data[2]) & 0x00000000000000ffL))) << 16) |
                        (((long) (((data[3]) & 0x00000000000000ffL))) << 24) |
                        (((long) (((data[4]) & 0x00000000000000ffL))) << 32) |
                        (((long) (((data[5]) & 0x00000000000000ffL))) << 40) |
                        (((long) (((data[6]) & 0x00000000000000ffL))) << 48) |
                        (((long) (((data[7]) & 0x00000000000000ffL))) << 56)
        );
    }
    //endregion


    /**
     * 打印字节
     *
     * @param sb
     * @param data
     */
    public void bytesToHexString(@NotNull StringBuilderT sb, byte[] data) {
        int hi, lo;
        final char[] HEX_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        int len = data.length;
        sb.clear();
        for (int i = 0; i < len; ++i) {
            hi = (data[i] >> 4) & 0x0000000F;
            lo = data[i] & 0x0000000F;
            sb.append(HEX_CHARS[hi]);
            sb.append(HEX_CHARS[lo]);
        }
    }
}
const FLAG = 255;
const LONFLAG = 255n;

//region 数字与字节转换
//short自动转换为整数
//先移位再并
function bswap16(val) {
    return [
        (val & FLAG),
        ((val >> 8) & FLAG)
    ];
};

function bswap32(val) {
    return [
        (val & FLAG),
        ((val >> 8) & FLAG),
        ((val >> 16) & FLAG),
        ((val >> 24) & FLAG)
    ];
};

//javascript不支持BigInt左移位,做系统设计时要注意这个问题,不要使用这个方法
function bswap64(val) {
    return [
        (val & LONFLAG),
        ((val >> 8) & LONFLAG),
        ((val >> 16) & LONFLAG),
        ((val >> 24) & LONFLAG),
        ((val >> 32) & LONFLAG),
        ((val >> 40) & LONFLAG),
        ((val >> 48) & LONFLAG),
        ((val >> 56) & LONFLAG)
    ];
};

//先并再移位
//因位操作符会隐式的自动转换类型,因此实际转换结果为32位整数,这点和java一样
function bytesToInt16(bytes) {
    return (
        ((bytes[0] & FLAG)) |
        (((bytes[1] & FLAG) << 8))
    );
};

function bytesToInt32(bytes) {
    return (
        ((bytes[0] & FLAG)) |
        ((((bytes[1] & FLAG) << 8))) |
        ((((bytes[2] & FLAG) << 16))) |
        ((((bytes[3] & FLAG) << 24)))
    );
};

//javascript不支持BigInt左移位,做系统设计时要注意这个问题,不要使用这个方法
function bytesToInt64(bytes) {
    return (
        ((BigInt(bytes[0]) & 255n) |
            ((BigInt(bytes[1]) & LONFLAG) << 8) |
            ((BigInt(bytes[2]) & LONFLAG) << 16) |
            ((BigInt(bytes[3]) & LONFLAG) << 24) |
            ((BigInt(bytes[4]) & LONFLAG) << 32) |
            ((BigInt(bytes[5]) & LONFLAG) << 40) |
            ((BigInt(bytes[6]) & LONFLAG) << 48) |
            ((BigInt(bytes[7]) & LONFLAG) << 56)
        )
    );
};
//endregion
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kmblack1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值