Conversion source code between byte and INT in Java:
// Mutual conversion between byte and INT public static byte inttobyte (int x) {return (byte) x;} public static int bytetoint (byte B) {// Java always treats byte as a byte; we can binary it and 0xff to get its unsigned value return B & 0xff ;}
Test code:
// Test int to byteint int0 = 234; byte byte0 = inttobyte (int0); system. out. println ("byte0 =" + byte0); // byte0 =-22 // test byte to intint int1 = bytetoint (byte0); system. out. println ("int1 =" + int1); // int1 = 234
Conversion source code between byte array and INT in Java:
// Mutual conversion between byte arrays and 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) {return New byte [] {(byte) (A> 24) & 0xff), (byte) (A> 16) & 0xff), (byte) (A> 8) & 0xff), (byte) (A & 0xff )};}
Test code:
// Test int to byte array int int2 = 1417; byte [] bytesint = inttobytearray (int2); system. out. println ("bytesint =" + bytesint); // bytesint = [B @ de6ced // test the conversion of byte array to intint int3 = bytearraytoint (bytesint); system. out. println ("int3 =" + int3); /// int3 = 1417
Conversion source code between byte array and long in Java:
Private Static bytebuffer buffer = bytebuffer. allocate (8); // mutual conversion between the byte array and 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 flip return buffer. getlong ();}
Test code:
// Test long to byte array long long1 = 2223; byte [] byteslong = longtobytes (long1); system. out. println ("bytes =" + byteslong); // bytes = [B @ c17164 // test the conversion of byte array to Longlong long2 = bytestolong (byteslong); system. out. println ("long2 =" + long2); // long2 = 2223
Overall tool source code:
Import Java. NIO. bytebuffer; public class test {Private Static bytebuffer buffer = bytebuffer. allocate (8); public static void main (string [] ARGs) {// test int to byteint int0 = 234; byte byte0 = inttobyte (int0); system. out. println ("byte0 =" + byte0); // byte0 =-22 // test byte to intint int1 = bytetoint (byte0); system. out. println ("int1 =" + int1); // int1 = 234 // test int to byte array int int2 = 1417; byte [] bytesint = inttobytearray (int2); system. out. println ("bytesint =" + bytesint); // bytesint = [B @ de6ced // test the conversion of byte array to intint int3 = bytearraytoint (bytesint); system. out. println ("int3 =" + int3); // int3 = 1417 // test the long to byte array long long1 = 2223; byte [] byteslong = longtobytes (long1); system. out. println ("bytes =" + byteslong); // bytes = [B @ c17164 // test the conversion of byte array to Longlong long2 = bytestolong (byteslong); system. out. println ("long2 =" + long2); // long2 = 2223} // mutual conversion between byte and INT public static byte inttobyte (int x) {return (byte) x ;} public static int bytetoint (byte B) {// Java always treats byte as a operator; we can perform binary and get its unsigned value return B & 0xff;} // the conversion between the array and INT public static int bytearraytoint (byte [] B) by binary and 0xff) {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) };}// mutual conversion between byte array and 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 flip return buffer. getlong ();}}
Test result:
Byte0 =-22
Int1 = 234
Bytesint = [B @ de6ced
Int3 = 1417
Bytes = [B @ c17164
Long2 = 2223
Reference Article 1: Workshop.
Refer to article 2: http://stackoverflow.com/questions/1921357/convert-integer-0000-byte-array-java.
Reference 3: Workshop.