inttobytes.java
来自「MM7彩信对接网关示例」· Java 代码 · 共 66 行
JAVA
66 行
/*
* Created on 2003-12-24
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package com.rainbow.util.tools;
/**
* @author liuhongshan
*
* 将整数转换成byte型数组,有big-endian,little-endian两种。
*/
public class IntToBytes {
//将int转换成byte数组,大端字节序
public static byte[] intToBytesB(int i) {
byte[] buf = new byte[4];
buf[0] = (byte) (((i >> 24) & 0x000000FF));
buf[1] = (byte) (((i >> 16) & 0x000000FF));
buf[2] = (byte) (((i >> 8) & 0x000000FF));
buf[3] = (byte) (((i) & 0x000000FF));
return buf;
}
// 将int转换成byte数组,小端字节序
public static byte[] intToBytesL(int i) {
byte[] buf = new byte[4];
buf[0] = (byte) (((i) & 0x000000FF));
buf[1] = (byte) (((i >> 8) & 0x000000FF));
buf[2] = (byte) (((i >> 16) & 0x000000FF));
buf[3] = (byte) (((i >> 24) & 0x000000FF));
return buf;
}
//改变字节序int
public static int alterEndianInt(int i) {
int j = 0;
j |= (i<<24)&0xFF000000;
j |= (i<<8)&0x00FF0000;
j |= (i>>8)&0x0000FF00;
j |= (i>>24)&0x00000FF;
return j;
}
// 改变字节序short
public static short alterEndianShort(short i) {
short j = 0;
j |= (i<<8)&0xFF00;
j |= (i>>8)&0x00FF;
return j;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?