The most comprehensive Java bytes byte operation, processing Java basic data conversion and conversion operation tools, streaming media and Java low-level development projects commonly used tools class

Source: Internet
Author: User

Introduction: Conversion and conversion manipulation tools for basic data processing in Java

First, the realization function

1, int pre-byte mutual transfer

2, int and byte[] turn each other

3, short and byte to turn each other

4, short and byte[] mutual transfer

5, 16 bit short and byte[] mutual transfer

6, long pre-byte[] Mutual transfer

7, byte[] and InputStream Mutual transfer

8, byte and string transfer

9, 16 binary character representable int

10, decimal to 2 binary

11, byte[] ext. 16 binary characters

12, byte[] array specified position extraction byte[]

Second, the 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 Transfer tool * @author Eguid * Eguid's official website: www.eguid.cc * Eguid's CSDN blog: http://blog.csdn.net/eguid_1,   Blog Park: Http://www.cnblogs.com/eguid */public class Byteutil {private static Bytebuffer buffer = Bytebuffer.allocate (8);/**      * int to BYTE * @param x * @return */public static byte inttobyte (int x) {return (byte) x; }/** * byte to int * @param b * @return */public static int Bytetoint (byte b) {//java byt      E is signed, converted to unsigned return B & 0xFF via &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; }/** * int to byte[] * @param A * @return */public static byte[] Inttobytearray (int a) {RET                 Urn New byte[] {(byte) ((a >>) & 0xFF), (Byte) ((a >> +) & 0xFF), (byte)      ((a >> 8) & 0xFF), (Byte) (A & 0xFF)}; }/** * Short turn 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); }/** * byte[] Turn short * * @param b * @param index * @return */public static short Bytearrtoshort (byte[] B, int. index) {return (short) (((B[index + 0] &l t;< 8) |      B[index + 1] & 0xff));  }/** * 16 bit short turn byte[] * * @param s * short * @return byte[] * * */Public          Static byte[] Shorttobytearr (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[] turn 16 bit short * @param b * @return */public static short Bytearrtoshort (byte[] b) {RET    Urn Bytearrtoshort (b,0);          }/** * Long turn byte[] * @param x * @return */public static byte[] Longtobytes (long x) {          Buffer.putlong (0, X);      return Buffer.array (); }/** * byte[] Go long * @param bytes * @return * * * public static longBytestolong (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;} /** * Stream converted to byte[] * @param instream * @return */public static byte[] Readinputstream (InputStream instream) {Bytearrayoutput  Stream 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[] Turn InputStream * @param b * @return */public static InputStream Readbytearr (byte[] b) {return new bytearrayinput Stream (b);} /** * The numbers within a 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;} /** * byte array converted to STIRNG * @param S1-array * @param encode-Character Set * @param err-conversion error when returning the literal * @return */public static String getst Ring (byte[] s1,string encode,string err) {try {return new String (S1,encode);} catch (Unsupportedencodingexception e) { return err==null?null:err;}} /** * byte array converted to STIRNG * @param s1-array * @param encode-Character set * @return */public static String getString (byte[] s1,string encod e) {return getString (s1,encode,null);} Test public static void main (String []args) {System.err.println (isEq (New Byte[]{1,2},new byte[]{1,2})); /** * byte array to 16 binary 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, +). substring (1); } return result; /** * 16 binary character Genesis int * @param hexstring * @return */public static int hexstringtoint (String hexstring) {return Integer.parsein T (hexstring,16);} /** * Decimal Turn binary * @param i * @return */public static String inttobinary (int i) {return integer.tobinarystring (i);}}



The most comprehensive Java bytes byte operation, processing Java basic data conversion and conversion operation tools, streaming media and Java low-level development projects commonly used tools class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.