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)};}//65279public 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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部