short,int,long与byte数组之间的转换
- 格式:doc
- 大小:48.50 KB
- 文档页数:4
java中int与byte,以及long与byte之间的转换原⽂地址:/zgyulongfei/article/details/7738970public class Utilities {public static byte[] int2Bytes(int num) {byte[] byteNum = new byte[4];for (int ix = 0; ix < 4; ++ix) {int offset = 32 - (ix + 1) * 8;byteNum[ix] = (byte) ((num >> offset) & 0xff);}return byteNum;}public static int bytes2Int(byte[] byteNum) {int num = 0;for (int ix = 0; ix < 4; ++ix) {num <<= 8;num |= (byteNum[ix] & 0xff);}return num;}public static byte int2OneByte(int num) {return (byte) (num & 0x000000ff);}public static int oneByte2Int(byte byteNum) {return byteNum > 0 ? byteNum : (128 + (128 + byteNum));}public static byte[] long2Bytes(long num) {byte[] byteNum = new byte[8];for (int ix = 0; ix < 8; ++ix) {int offset = 64 - (ix + 1) * 8;byteNum[ix] = (byte) ((num >> offset) & 0xff);}return byteNum;}public static long bytes2Long(byte[] byteNum) {long num = 0;for (int ix = 0; ix < 8; ++ix) {num <<= 8;num |= (byteNum[ix] & 0xff);}return num;}public static void main(String[] args) {int num = 129;System.out.println("测试的int值为:" + num);byte[] int2bytes = Utilities.int2Bytes(num);System.out.printf("int转成bytes: ");for (int i = 0; i < 4; ++i) {System.out.print(int2bytes[i] + " ");}System.out.println();int bytes2int = Utilities.bytes2Int(int2bytes);System.out.println("bytes转⾏成int: " + bytes2int);byte int2OneByte = Utilities.int2OneByte(num);System.out.println("int转⾏成one byte: " + int2OneByte);int oneByte2Int = Utilities.oneByte2Int(int2OneByte);System.out.println("one byte转⾏成int: " + oneByte2Int);System.out.println();long longNum = 100000;System.out.println("测试的long值为:" + longNum);byte[] long2Bytes = Utilities.long2Bytes(longNum);System.out.printf("long转⾏成bytes: ");for (int ix = 0; ix < long2Bytes.length; ++ix) {System.out.print(long2Bytes[ix] + " ");}System.out.println();long bytes2Long = Utilities.bytes2Long(long2Bytes); System.out.println("bytes转⾏成long: " + bytes2Long); }}测试的int值为:129int转成bytes: 0 0 0 -127bytes转⾏成int: 129int转⾏成one byte: -127one byte转⾏成int: 129测试的long值为:100000long转⾏成bytes: 0 0 0 0 0 1 -122 -96bytes转⾏成long: 100000。
javabyte数组与int,long,short,byte的转换实现⽅法实例如下:public class DataTypeChangeHelper {/*** 将⼀个单字节的byte转换成32位的int** @param b* byte* @return convert result*/public static int unsignedByteToInt(byte b) {return (int) b & 0xFF;}/*** 将⼀个单字节的Byte转换成⼗六进制的数** @param b* byte* @return convert result*/public static String byteToHex(byte b) {int i = b & 0xFF;return Integer.toHexString(i);}/*** 将⼀个4byte的数组转换成32位的int** @param buf* bytes buffer* @param byte[]中开始转换的位置* @return convert result*/public static long unsigned4BytesToInt(byte[] buf, int pos) {int firstByte = 0;int secondByte = 0;int thirdByte = 0;int fourthByte = 0;int index = pos;firstByte = (0x000000FF & ((int) buf[index]));secondByte = (0x000000FF & ((int) buf[index + 1]));thirdByte = (0x000000FF & ((int) buf[index + 2]));fourthByte = (0x000000FF & ((int) buf[index + 3]));index = index + 4;return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;}/*** 将16位的short转换成byte数组** @param s* short* @return byte[] 长度为2* */public static byte[] shortToByteArray(short s) {byte[] targets = new byte[2];for (int i = 0; i < 2; i++) {int offset = (targets.length - 1 - i) * 8;targets[i] = (byte) ((s >>> offset) & 0xff);}return targets;}/*** 将32位整数转换成长度为4的byte数组** @param s* int* @return byte[]* */public static byte[] intToByteArray(int s) {byte[] targets = new byte[2];for (int i = 0; i < 4; i++) {int offset = (targets.length - 1 - i) * 8;targets[i] = (byte) ((s >>> offset) & 0xff);}return targets;}/*** long to byte[]** @param s* long* @return byte[]* */public static byte[] longToByteArray(long s) {byte[] targets = new byte[2];for (int i = 0; i < 8; i++) {int offset = (targets.length - 1 - i) * 8;targets[i] = (byte) ((s >>> offset) & 0xff);}return targets;}/**32位int转byte[]*/public static byte[] int2byte(int res) {byte[] targets = new byte[4];targets[0] = (byte) (res & 0xff);// 最低位targets[1] = (byte) ((res >> 8) & 0xff);// 次低位targets[2] = (byte) ((res >> 16) & 0xff);// 次⾼位targets[3] = (byte) (res >>> 24);// 最⾼位,⽆符号右移。
Java中byte、byte数组与int、long的转换详解⼀、Java 中 byte 和 int 之间的转换源码://byte 与 int 的相互转换public static byte intToByte(int x) {return (byte) x;}public static int byteToInt(byte b) {//Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进⾏⼆进制与得到它的⽆符值return b & 0xFF;}测试代码://测试 int 转 byteint int0 = 234;byte byte0 = intToByte(int0);System.out.println("byte0=" + byte0);//byte0=-22//测试 byte 转 intint int1 = byteToInt(byte0);System.out.println("int1=" + int1);//int1=234⼆、Java 中 byte 数组和 int 之间的转换源码://byte 数组与 int 的相互转换public static int byteArrayToInt(byte[] b) {return b[3] & 0xFF |(b[2] & 0xFF) << 8 |(b[1] & 0xFF) << 16 |(b[0] & 0xFF) << 24;}public static byte[] intToByteArray(int a) {return new byte[] {(byte) ((a >> 24) & 0xFF),(byte) ((a >> 16) & 0xFF),(byte) ((a >> 8) & 0xFF),(byte) (a & 0xFF)};}测试代码://测试 int 转 byte 数组int int2 = 1417;byte[] bytesInt = intToByteArray(int2);System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced//测试 byte 数组转 intint int3 = byteArrayToInt(bytesInt);System.out.println("int3=" + int3);//int3=1417三、Java 中 byte 数组和 long 之间的转换源码:private static ByteBuffer buffer = ByteBuffer.allocate(8);//byte 数组与 long 的相互转换public static byte[] longToBytes(long x) {buffer.putLong(0, x);return buffer.array();}public static long bytesToLong(byte[] bytes) {buffer.put(bytes, 0, bytes.length);buffer.flip();//need flipreturn buffer.getLong();}测试代码://测试 long 转 byte 数组long long1 = 2223;byte[] bytesLong = longToBytes(long1);System.out.println("bytes=" + bytesLong);//bytes=[B@c17164//测试 byte 数组转 longlong long2 = bytesToLong(bytesLong);System.out.println("long2=" + long2);//long2=2223四、整体⼯具类源码:import java.nio.ByteBuffer;public class Test {private static ByteBuffer buffer = ByteBuffer.allocate(8);public static void main(String[] args) {//测试 int 转 byteint int0 = 234;byte byte0 = intToByte(int0);System.out.println("byte0=" + byte0);//byte0=-22//测试 byte 转 intint int1 = byteToInt(byte0);System.out.println("int1=" + int1);//int1=234//测试 int 转 byte 数组int int2 = 1417;byte[] bytesInt = intToByteArray(int2);System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced//测试 byte 数组转 intint int3 = byteArrayToInt(bytesInt);System.out.println("int3=" + int3);//int3=1417//测试 long 转 byte 数组long long1 = 2223;byte[] bytesLong = longToBytes(long1);System.out.println("bytes=" + bytesLong);//bytes=[B@c17164//测试 byte 数组转 longlong long2 = bytesToLong(bytesLong);System.out.println("long2=" + long2);//long2=2223}//byte 与 int 的相互转换public static byte intToByte(int x) {return (byte) x;}public static int byteToInt(byte b) {//Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进⾏⼆进制与得到它的⽆符值 return b & 0xFF;}//byte 数组与 int 的相互转换public static int byteArrayToInt(byte[] b) {return b[3] & 0xFF |(b[2] & 0xFF) << 8 |(b[1] & 0xFF) << 16 |(b[0] & 0xFF) << 24;}public static byte[] intToByteArray(int a) {return new byte[] {(byte) ((a >> 24) & 0xFF),(byte) ((a >> 16) & 0xFF),(byte) ((a >> 8) & 0xFF),(byte) (a & 0xFF)};}//byte 数组与 long 的相互转换public static byte[] longToBytes(long x) {buffer.putLong(0, x);return buffer.array();}public static long bytesToLong(byte[] bytes) {buffer.put(bytes, 0, bytes.length);buffer.flip();//need flipreturn buffer.getLong();}}运⾏测试结果:byte0=-22int1=234bytesInt=[B@de6cedint3=1417bytes=[B@c17164long2=2223总结以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,如果有疑问⼤家可以留⾔交流。
java基本数据类型之间的转换Java是一门强类型语言,变量需要明确指定其数据类型。
Java中含有8个基本数据类型,它们是boolean、byte、short、int、long、float、double和char。
在Java编程中,有时也需要对这些基本数据类型进行一些转换。
本文将围绕Java基本数据类型之间的转换展开。
一、自动类型转换Java中可以将一种数据类型的变量赋值给另一种数据类型的变量,这种转换称为自动类型转换。
自动类型转换是指从小类型到大类型的转换过程, Java在内部进行转换,无需开发人员进行显式的操作。
例如,将一个byte类型的变量赋值给int类型的变量:byte b = 10;int i = b;在这个过程中,Java自动将byte类型的变量b转换成int类型,并将其赋值给i。
二、强制类型转换有时需要对一个变量强制转换为另一种类型,这种转换称为强制类型转换。
强制类型转换是从大类型到小类型的转换过程,在进行强制类型转换时,需要在转换前使用小括号指定要转换的类型。
例如,将一个double类型的变量强制转换成int类型:double d = 10.5;int i = (int)d;在这个过程中,Java会将double类型的变量d转换成int类型,并将其赋值给i。
需要注意的是,在进行强制类型转换时,可能会出现数据精度丢失的情况。
三、字符类型转换在Java中,char类型可以被当做数字来处理,它与int类型可以互相转换。
在Java中,可以使用强制类型转换将字符类型转换成整型,例如:char c = 'a';int i = (int)c;在这个过程中,字符'a'会被转换成对应的ASCII码97。
四、字符串类型转换Java中的字符串类型与其他基本数据类型之间的转换需要借助于包装类。
Java中含有六个与基本数据类型对应的包装类,它们是Boolean、Byte、Short、Integer、Long、Float、Double和Character。
int类型转换byte类型计算机中,int类型占⽤4个字节,byte类型占⽤1个字节;当int类型强转为byte类型时,计算机会截取最后的⼋位(1个字节);由于计算机存储数据时,都是以补码的形式进⾏存储。
然⽽,我们通常看到的数却是计算机存储的补码先转换成反码,后转换成原码,再转换成⼗进制呈现的。
原码、反码与补码的关系:正数:原码 = 反码 = 补码负数:原码取反 = 反码(符号位不变);反码 + 1 = 补码(符号位上的进位舍弃)举例:int a = 128,转换成⼆进制形式是0000 0000 0000 0000 0000 0000 1000 0000,由于正数的原码=反码=补码,因此计算机存储的是0000 0000 0000 0000 0000 0000 1000 0000。
int a = -128,转换成⼆进制形式是1000 0000 0000 0000 0000 0000 1000 0000,由于负数的原码、反码与补码的转换关系是:原码取反=反码(符号位不变),反码+1=补码;反码:1111 1111 1111 1111 1111 1111 0111 1111补码:1111 1111 1111 1111 1111 1111 1000 0000因此,在计算机中存储的是1111 1111 1111 1111 1111 1111 1000 0000int a = 128;byte b = (byte) a; // b=-128⾸先,由上述第⼀个例⼦得知,128在计算机中存储的补码形式为0000 0000 0000 0000 0000 0000 1000 0000,此时强制转换成byte类型的数据时,计算机会⾃动截取最后的⼋位(1个字节)1000 0000,由补码最⾼位为1得知,转换后的数据是⼀个负数,根据负数补码求反码,我们可以得到该数的反码是1111 1111,根据负数反码求原码,可得到该数的原码是1000 0000。
byte[]数组和int之间的转换这⾥简单记录下两种转换⽅式:第⼀种:1、int与byte[]之间的转换(类似的byte short,long型)[java]1. /**2. * 将int数值转换为占四个字节的byte数组,本⽅法适⽤于(低位在前,⾼位在后)的顺序。
和bytesToInt()配套使⽤3. * @param value4. * 要转换的int值5. * @return byte数组6. */7. public static byte[] intToBytes( int value )8. {9. byte[] src = new byte[4];10. src[3] = (byte) ((value>>24) & 0xFF);11. src[2] = (byte) ((value>>16) & 0xFF);12. src[1] = (byte) ((value>>8) & 0xFF);13. src[0] = (byte) (value & 0xFF);14. return src;15. }16. /**17. * 将int数值转换为占四个字节的byte数组,本⽅法适⽤于(⾼位在前,低位在后)的顺序。
和bytesToInt2()配套使⽤18. */19. public static byte[] intToBytes2(int value)20. {21. byte[] src = new byte[4];22. src[0] = (byte) ((value>>24) & 0xFF);23. src[1] = (byte) ((value>>16)& 0xFF);24. src[2] = (byte) ((value>>8)&0xFF);25. src[3] = (byte) (value & 0xFF);26. return src;27. }byte[]转int[java]1. /**2. * byte数组中取int数值,本⽅法适⽤于(低位在前,⾼位在后)的顺序,和和intToBytes()配套使⽤3. *4. * @param src5. * byte数组6. * @param offset7. * 从数组的第offset位开始8. * @return int数值9. */10. public static int bytesToInt(byte[] src, int offset) {11. int value;12. value = (int) ((src[offset] & 0xFF)13. | ((src[offset+1] & 0xFF)<<8)14. | ((src[offset+2] & 0xFF)<<16)15. | ((src[offset+3] & 0xFF)<<24));16. return value;17. }18.19. /**20. * byte数组中取int数值,本⽅法适⽤于(低位在后,⾼位在前)的顺序。
1字节序由于不同的计算机系统采用不同的字节序存储数据,同样一个4字节的32位整数,在内存中存储的方式就不同. 字节序分为小尾字节序(Little Endian)和大尾字节序(Big Endian), Intel处理器大多数使用小尾字节序, Motorola处理器大多数使用大尾(Big Endian)字节序;小尾就是低位字节排放在内存的低端,高位字节排放在内存的高端。
例如一个4字节的值为0x1234567的整数与高低字节对应关系:01234567Byte3Byte2Byte1Byte0高位字节--à---------à--------------à低位字节将在内存中按照如下顺序排放:内存地址序号字节在内存中的地址16进制值0x03Byte3010x02Byte2230x01Byte1450x00Byte067大尾就是高位字节排放在内存的低端,低位字节排放在内存的高端。
例如一个4字节的值为0x1234567的整数与高低字节对应关系:01234567Byte3Byte2Byte1Byte0高位字节--à---------à--------------à低位字节将在内存中按照如下顺序排放:内存地址序号字节在内存中的地址16进制值0x03Byte0670x02Byte1450x01Byte2230x00Byte3012网络字节序TCP/IP各层协议将字节序定义为大尾,因此TCP/IP协议中使用的字节序通常称之为网络字节序。
3字串在内存中的存储(intel系列)字串和整数是相反的,是安字串的索引从低到高存储到内存中的;char s[4] = “abc”;a b c\0s[0]s[1]s[2]s[3]将在内存中按照如下顺序排放:内存地址序号16进制值指针P的位置0xbffeadf7\0p+30xbffeadf6c p+20xbffeadf5b p+10xbffeadf4a pint main(void){char s[4] = "abc";char *p = s;printf("%02x, %02x, %02x, %02x\n", &s[0], &s[1], &s[2], &s[3]);printf("%02x, %02x, %02x, %02x\n", p, p+1, p+2, p+3);printf("%c, %c, %c, %c\n", s[0], s[1], s[2], s[3]);return 0;}输出结果:[netcool@HFINMSP2 demo]$ ./demo001bffeadf4, bffeadf5, bffeadf6, bffeadf7bffeadf4, bffeadf5, bffeadf6, bffeadf7a, b, c,4整数数组在内存中的存储(intel系列)同字串一样,但是数组里的每一个整数的存储是按照小尾字节序;5linux系统中的处理方法网络字节序作为一个标准字节序,如果系统并没有提供相关的转换函数,我们可以通过以下4个宏实现本地字节序和网络字节序的相互转换:htons():将16位无符号整数从本地字节序转换成网络字节序htonl():将32位无符号整数从本地字节序转换成网络字节序ntohs():将16位无符号整数从网络字节序转换成本地字节序ntohl():将32位无符号整数从网络字节序转换成本地字节序网络字节序与主机字节序不同的CPU有不同的字节序类型这些字节序是指整数在内存中保存的顺序这个叫做主机序最常见的有两种1.Little endian:将低序字节存储在起始地址2.Big endian:将高序字节存储在起始地址LE little-endian最符合人的思维的字节序地址低位存储值的低位地址高位存储值的高位怎么讲是最符合人的思维的字节序,是因为从人的第一观感来说低位值小,就应该放在内存地址小的地方,也即内存地址低位反之,高位值就应该放在内存地址大的地方,也即内存地址高位BE big-endian最直观的字节序地址低位存储值的高位地址高位存储值的低位为什么说直观,不要考虑对应关系只需要把内存地址从左到右按照由低到高的顺序写出把值按照通常的高位到低位的顺序写出两者对照,一个字节一个字节的填充进去例子:在内存中双字0x01020304(DWORD)的存储方式内存地址4000 4001 4002 4003LE 04 03 02 01BE 01 02 03 04例子:如果我们将0x1234abcd写入到以0x0000开始的内存中,则结果为big-endian little-endian0x0000 0x12 0xcd0x0001 0x23 0xab0x0002 0xab 0x340x0003 0xcd 0x12x86系列CPU都是little-endian的字节序.网络字节顺序是TCP/IP中规定好的一种数据表示格式,它与具体的CPU类型、操作系统等无关,从而可以保证数据在不同主机之间传输时能够被正确解释。
byte数组与byte数组转化摘要:一、引言二、byte数组与byte数组转化的概念1.byte数组2.byte数组转化三、byte数组与byte数组转化的方法1.字节数组转字符串2.字符串转字节数组四、byte数组与byte数组转化的应用场景1.网络传输2.文件存储五、总结正文:一、引言在计算机编程中,byte数组和byte数组转化是经常遇到的操作。
了解byte数组与byte数组转化的概念、方法和应用场景,对于编程工作非常有帮助。
二、byte数组与byte数组转化的概念1.byte数组byte数组,又称字节数组,是一种数据类型,用于存储一系列字节。
在Java、C#等编程语言中,它通常用于存储和处理二进制数据。
2.byte数组转化byte数组转化是指将byte数组与其他数据类型(如字符串、整数等)之间进行转换。
三、byte数组与byte数组转化的方法1.字节数组转字符串在Java中,可以使用`new String(byte[], charset)`方法将byte数组转换为字符串。
其中,`charset`表示字符集,如UTF-8、GBK等。
2.字符串转字节数组在Java中,可以使用`String.getBytes(charset)`方法将字符串转换为byte数组。
其中,`charset`表示字符集,如UTF-8、GBK等。
四、byte数组与byte数组转化的应用场景1.网络传输在网络传输过程中,数据通常以byte数组的形式进行传输。
因此,在处理网络数据时,需要将字符串、整数等数据类型转换为byte数组,以便进行传输。
2.文件存储在文件存储过程中,数据也需要以byte数组的形式进行存储。
例如,在将文本文件存储到磁盘时,需要将字符串转换为byte数组,然后将byte数组写入文件。
五、总结byte数组与byte数组转化是计算机编程中常见的操作。
java中long,int,short与byte数组之间的转换//long类型转成byte数组public static byte[] longToByte(long number) {long temp = number;byte[] b = new byte[8];for (int i = 0; i < b.length; i++) {b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位temp = temp >> 8; // 向右移8位}return b;}//byte数组转成longpublic static long byteToLong(byte[] b) {long s = 0;long s0 = b[0] & 0xff;// 最低位long s1 = b[1] & 0xff;long s2 = b[2] & 0xff;long s3 = b[3] & 0xff;long s4 = b[4] & 0xff;// 最低位long s5 = b[5] & 0xff;long s6 = b[6] & 0xff;long s7 = b[7] & 0xff;// s0不变s1 <<= 8;s2 <<= 16;s3 <<= 24;s4 <<= 8 * 4;s5 <<= 8 * 5;s6 <<= 8 * 6;s7 <<= 8 * 7;s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;return s;}/*** 注释:int到字节数组的转换!** @param number* @return*/public static byte[] intToByte(int number) {int temp = number;byte[] b = new byte[4];for (int i = 0; i < b.length; i++) {b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位temp = temp >> 8; // 向右移8位}return b;}/*** 注释:字节数组到int的转换!** @param b* @return*/public static int byteToInt(byte[] b) {int s = 0;int s0 = b[0] & 0xff;// 最低位int s1 = b[1] & 0xff;int s2 = b[2] & 0xff;int s3 = b[3] & 0xff;s3 <<= 24;s2 <<= 16;s1 <<= 8;s = s0 | s1 | s2 | s3;return s;}/*** 注释:short到字节数组的转换!** @param s* @return*/public static byte[] shortToByte(short number) {int temp = number;byte[] b = new byte[2];for (int i = 0; i < b.length; i++) {b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位temp = temp >> 8; // 向右移8位}return b;}/*** 注释:字节数组到short的转换!** @param b* @return*/public static short byteToShort(byte[] b) { short s = 0;short s0 = (short) (b[0] & 0xff);// 最低位 short s1 = (short) (b[1] & 0xff); s1 <<= 8;s = (short) (s0 | s1);return s;}。
1.2.package com.test;3.4.import java.nio.ByteBuffer;5.6.public class ByteUtil {7.8./**9. * @param args10. */11. public static void main(String[] args) {12. test2();13. }14. public static void test2()15. {16. short s = -20;17. byte[] b = new byte[2];18. putReverseBytesShort(b, s, 0);19. ByteBuffer buf = ByteBuffer.allocate(2);20. buf.put(b);21. buf.flip();22. System.out.println(getReverseBytesShort(b, 0));23. System.out.println(Short.reverseBytes(buf.getShort()));24. System.out.println("***************************");25. int i = -40;26. b = new byte[4];27. putReverseBytesInt(b, i, 0);28. buf = ByteBuffer.allocate(4);29. buf.put(b);30. buf.flip();31. System.out.println(getReverseBytesInt(b, 0));32. System.out.println(Integer.reverseBytes(buf.getInt()));33. System.out.println("***************************");34. long l = -50;35. b = new byte[8];36. putReverseBytesLong(b, l, 0);37. buf = ByteBuffer.allocate(8);38. buf.put(b);39. buf.flip();40. System.out.println(getReverseBytesLong(b, 0));41. System.out.println(Long.reverseBytes(buf.getLong()));42. System.out.println("***************************");43. }44. public static void test1()45. {46. short s = -20;47. byte[] b = new byte[2];48. putShort(b, s, 0);49. ByteBuffer buf = ByteBuffer.allocate(2);50. buf.put(b);51. buf.flip();52. System.out.println(getShort(b, 0));53. System.out.println(buf.getShort());54. System.out.println("***************************");55. int i = -40;56. b = new byte[4];57. putInt(b, i, 0);58. buf = ByteBuffer.allocate(4);59. buf.put(b);60. buf.flip();61. System.out.println(getInt(b, 0));62. System.out.println(buf.getInt());63. System.out.println("***************************");64. long l = -50;65. b = new byte[8];66. putLong(b, l, 0);67. buf = ByteBuffer.allocate(8);68. buf.put(b);69. buf.flip();70. System.out.println(getLong(b, 0));71. System.out.println(buf.getLong());72. System.out.println("***************************");73. }74. public static void putShort(byte b[], short s, int index){75. b[index] = (byte) (s >> 8);76. b[index + 1] = (byte) (s >> 0);77. }78. public static void putReverseBytesShort(byte b[], short s,int index) {79. b[index] = (byte) (s >> 0);80. b[index + 1] = (byte) (s >> 8);81. }82. public static short getShort(byte[] b, int index) {83. return (short) (((b[index] << 8) | b[index + 1] & 0xff));84. }85. public static short getReverseBytesShort(byte[] b, int index) {86. return (short) (((b[index+1] << 8) | b[index] & 0xff));87. }88.89. // ///////////////////////////////////////////////////////90. public static void putInt(byte[] bb, int x, int index) {91. bb[index + 0] = (byte) (x >> 24);92. bb[index + 1] = (byte) (x >> 16);93. bb[index + 2] = (byte) (x >> 8);94. bb[index + 3] = (byte) (x >> 0);95. }96. public static void putReverseBytesInt(byte[] bb, int x, intindex) {97. bb[index + 3] = (byte) (x >> 24);98. bb[index + 2] = (byte) (x >> 16);99. bb[index + 1] = (byte) (x >> 8);100. bb[index + 0] = (byte) (x >> 0);101. }102.103.public static int getInt(byte[] bb, int index) {104.return (int) ((((bb[index + 0] & 0xff) << 24) 105. | ((bb[index + 1] & 0xff) << 16)106. | ((bb[index + 2] & 0xff) << 8) | ((bb[ind ex + 3] & 0xff) << 0)));107. }108.public static int getReverseBytesInt(byte[] bb, int in dex) {109.return (int) ((((bb[index + 3] & 0xff) << 24) 110. | ((bb[index + 2] & 0xff) << 16)111. | ((bb[index + 1] & 0xff) << 8) | ((bb[ind ex + 0] & 0xff) << 0)));112. }113.114.// /////////////////////////////////////////////////// //////115.public static void putLong(byte[] bb, long x, int inde x) {116. bb[index + 0] = (byte) (x >> 56);117. bb[index + 1] = (byte) (x >> 48);118. bb[index + 2] = (byte) (x >> 40);119. bb[index + 3] = (byte) (x >> 32);120. bb[index + 4] = (byte) (x >> 24);121. bb[index + 5] = (byte) (x >> 16);122. bb[index + 6] = (byte) (x >> 8);123. bb[index + 7] = (byte) (x >> 0);124. }125.public static void putReverseBytesLong(byte[] bb, long x, int index) {126. bb[index + 7] = (byte) (x >> 56);127. bb[index + 6] = (byte) (x >> 48);128. bb[index + 5] = (byte) (x >> 40);129. bb[index + 4] = (byte) (x >> 32);130. bb[index + 3] = (byte) (x >> 24);131. bb[index + 2] = (byte) (x >> 16);132. bb[index + 1] = (byte) (x >> 8);133. bb[index + 0] = (byte) (x >> 0);134. }135.136.public static long getLong(byte[] bb, int index) { 137.return ((((long) bb[index + 0] & 0xff) << 56) 138. | (((long) bb[index + 1] & 0xff) << 48) 139. | (((long) bb[index + 2] & 0xff) << 40) 140. | (((long) bb[index + 3] & 0xff) << 32) 141. | (((long) bb[index + 4] & 0xff) << 24) 142. | (((long) bb[index + 5] & 0xff) << 16) 143. | (((long) bb[index + 6] & 0xff) << 8) | ( ((long) bb[index + 7] & 0xff) << 0));144. }145.public static long getReverseBytesLong(byte[] bb, int index) {146.return ((((long) bb[index + 7] & 0xff) << 56) 147. | (((long) bb[index + 6] & 0xff) << 48) 148. | (((long) bb[index + 5] & 0xff) << 40) 149. | (((long) bb[index + 4] & 0xff) << 32) 150. | (((long) bb[index + 3] & 0xff) << 24) 151. | (((long) bb[index + 2] & 0xff) << 16) 152. | (((long) bb[index + 1] & 0xff) << 8) | ( ((long) bb[index + 0] & 0xff) << 0));153. }154.}。