The most comprehensive Java byte operations, conversion and hexadecimal conversion tools for processing basic Java data, common tools for streaming media and underlying java development projects, and javabyte tools
Conversion and hexadecimal conversion tools used to process basic Java data
I. Implementation Functions
1. int pre-byte Conversion
2. Mutual conversion between int and byte []
3. Switch between short and byte
4. Switch between short and byte []
5. Switch between 16-bit short and byte []
6. long pre-byte [] interchange
7. Exchange between byte [] and inputstream
8. Conversion between byte and String
9. Convert hexadecimal to int
10. Convert decimal to binary
11. byte [] to hexadecimal characters
12. byte [] array specified position extraction byte []
Ii. Code Implementation
Package cc. eguid. util; import java. io. byteArrayInputStream; import java. io. byteArrayOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. unsupportedEncodingException; import java. nio. byteBuffer;/*** basic data interchange tool * @ author eguid * official website of eguid: www. eguid. cc * eguid's csdn blog: http://blog.csdn.net/eguid_1 blog: http://www.cnblogs.com/eguid */public class ByteUtil {private static ByteBuffe R buffer = ByteBuffer. allocate (8);/*** int to byte * @ param x * @ return */public static byte intToByte (int x) {return (byte) x ;} /*** convert byte to int * @ param B * @ return */public static int byteToInt (byte B) {// byte of Java is signed, convert & 0xFF to unsigned return B & 0xFF;}/*** byte [] to int * @ param B * @ return */public static int byteArrayToInt (byte [] B) {return B [3] & 0xFF | (B [2] & 0xFF) <8 | (B [1] & 0xFF) <<16 | (B [0] & 0xFF) <24;} public static int byteArrayToInt (byte [] B, int index) {return B [index + 3] & 0xFF | (B [index + 2] & 0xFF) <8 | (B [index + 1] & 0xFF) <16 | (B [index + 0] & 0xFF) <24 ;} /*** convert int to byte [] * @ param a * @ return */public static byte [] intToByteArray (int a) {return new byte [] {(byte) (a> 24) & 0xFF), (byte) (a> 16) & 0xFF), (byte) (a> 8) & 0xFF ), (byte) (a & 0xFF) };}/*** Convert short to byte [] ** @ param B * @ param s * @ param index */public static void byteArrToShort (byte B [], short s, int index) {B [index + 1] = (byte) (s> 8); B [index + 0] = (byte) (s> 0 );} /*** convert byte [] to short ** @ param B * @ param index * @ return */public static short byteArrToShort (byte [] B, int index) {return (short) (B [index + 0] <8) | B [index + 1] & 0xff ));} /*** convert 16-bit short to byte [] ** @ Param s * short * @ return byte [] **/public static byte [] export tobytearr (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 ;} /*** byte [] to 16-bit short * @ param B * @ return */public static short byteArrToShort (byte [] B) {return byteArrToShort (B, 0 );} /*** convert long to byte [] * @ Param x * @ return */public static byte [] longToBytes (long x) {buffer. putLong (0, x); return buffer. array ();}/*** byte [] to Long * @ param bytes * @ return */public static long bytesToLong (byte [] bytes) {buffer. put (bytes, 0, bytes. length); buffer. flip (); // need flip return buffer. getLong ();} /*** extract new byte [] From byte [] * @ param data-Metadata * @ param start-start position * @ param end-end position * @ return New byte [] */Public static byte [] getByteArr (byte [] data, int start, int end) {byte [] ret = new byte [end-start]; for (int I = 0; (start + I) <end; I ++) {ret [I] = data [start + I];} return ret ;} /*** convert the stream to byte [] * @ param inStream * @ return */public static byte [] readInputStream (InputStream inStream) {ByteArrayOutputStream outStream = null; try {outStream = new ByteArrayOutputStream (); byte [] buffer = new byte [1024]; byte [] data = Null; int len = 0; while (len = inStream. read (buffer ))! =-1) {outStream. write (buffer, 0, len);} data = outStream. toByteArray (); return data;} catch (IOException e) {return null;} finally {try {if (outStream! = Null) {outStream. close ();} if (inStream! = Null) {inStream. close () ;}} catch (IOException e) {return null ;}}} /*** byte [] to inputstream * @ param B * @ return */public static InputStream readByteArr (byte [] B) {return new ByteArrayInputStream (B );} /*** whether the numbers in the byte array are the same * @ param s1 * @ param s2 * @ return */public static boolean isEq (byte [] s1, byte [] s2) {int slen = s1.length; if (slen = s2.length) {for (int index = 0; index <slen; index ++) {if (s1 [index]! = S2 [index]) {return false;} return true;} return false ;} /*** convert byte array to Stirng * @ param s1-array * @ param encode-Character Set * @ param err-return the text * @ return */public static String when the conversion error occurs. getString (byte [] s1, string encode, String err) {try {return new String (s1, encode);} catch (UnsupportedEncodingException e) {return err = null? Null: err ;}} /*** convert byte array to Stirng * @ param s1-array * @ param encode-Character Set * @ return */public static String getString (byte [] s1, String encode) {return getString (s1, encode, null);} // test public static void main (String [] args) {System. err. println (isEq (new byte [] {1, 2}, new byte [] {1, 2 }));} /*** convert the byte array to a hexadecimal String * @ param B * @ return */public static String byteArrToHexString (byte [] B) {String result = ""; for (int I = 0; I <B. length; I ++) {result + = Integer. toString (B [I] & 0xff) + 0x100, 16 ). substring (1);} return result;}/*** hexadecimal character conversion int * @ param hexString * @ return */public static int hexStringToInt (String hexString) {return Integer. parseInt (hexString, 16);}/*** convert to binary in decimal format * @ param I * @ return */public static String intToBinary (int I) {return Integer. toBinaryString (I );}}