发一个JAVA将小端序转大端序的代码块。。。
另外2个整型的方法
大端序把最后返回的顺序改下即可。
public static short LowToShort(short a) { return (short)(((a & 0xFF) << 8) | ((a >> 8) & 0xFF)); } public static int LowToInt(int a) { return (((a & 0xFF) << 24) | (((a >>8) & 0xFF) << 16) | (((a >> 16) & 0xFF) << 8) | ((a >> 24) & 0xFF)); }
public static short LowToShort(short a) { return (short) (((a & 0xFF) << 8) | ((a >> 8) & 0xFF)); } public static int LowToInt(int a) { return (((a & 0xFF) << 24) | (((a >> 8) & 0xFF) << 16) | (((a >> 16) & 0xFF) << 8) | ((a >> 24) & 0xFF)); }
另外2个整型的方法
public static void printLittleEndian(int i) { byte[] b = new byte[4]; b[0] = (byte) (0xff & i); b[1] = (byte) ((0xff00 & i) >> 8); b[2] = (byte) ((0xff0000 & i) >> 16); b[3] = (byte) ((0xff000000 & i) >> 24); for (byte c : b) { System.out.print("0x" + Integer.toHexString(c & 0xFF) + ","); } System.out.println(); } public static void printBigEndian(int i) { byte[] b = new byte[4]; b[3] = (byte) (0xff & i); b[2] = (byte) ((0xff00 & i) >> 8); b[1] = (byte) ((0xff0000 & i) >> 16); b[0] = (byte) ((0xff000000 & i) >> 24); for (byte c : b) { System.out.print("0x" + Integer.toHexString(c & 0xFF) + ","); } System.out.println(); } public static void printBigEndian2(int i) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); try { dos.writeInt(i); dos.flush(); bos.flush(); byte[] data = bos.toByteArray(); dos.close(); bos.close(); for (byte c : data) { System.out.print("0x" + Integer.toHexString(c & 0xFF) + ","); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static int toLittleEndian(int i) { byte[] b = new byte[4]; b[0] = (byte) (0xff & i); b[1] = (byte) ((0xff00 & i) >> 8); b[2] = (byte) ((0xff0000 & i) >> 16); b[3] = (byte) ((0xff000000 & i) >> 24); return b[3] & 0xFF | (b[2] & 0xFF) << 8 | (b[1] & 0xFF) << 16 | (b[0] & 0xFF) << 24; }
大端序把最后返回的顺序改下即可。
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2312
- 用户1336
- 访客11621415
每日一句
Compliment yourself daily.
每天夸自己一句。
每天夸自己一句。
How to Ungroup Icons on Windows 11 Taskbar With a Registry Hack (and 2 More Ways)
反编译修改class文件变量
如何在大学成为一名优秀的程序员?
VMware NAT端口映射外网访问虚拟机linux
ubuntu下提取DSDT SSDT
使用HTML和CSS设计磨砂玻璃效果
解决android studio 4.4使用javah失败
vscode使用eslint自动代码格式化
c++浮点运算能力附安卓版
【源码】两种仪表盘
P2P中NAT之间的打洞可能性
jQuery的load方法Cannot read property 'indexOf' of undefined
【开源Roguelike游戏】素地牢源码
新会员