The implementation method of Java byte array and int,long,short,byte transformation _java

Source: Internet
Author: User

The

instance is as follows:

public class Datatypechangehelper {/** * Converts a single-byte byte to a 32-bit int * * @param b * byte * @return 
  Convert result */public static int Unsignedbytetoint (byte b) {return (int) B & 0xFF; /** * Converts a single-byte byte to a number of 16 bytes * * @param b * byte * @return convert result/public s 
    Tatic String Bytetohex (byte b) {int i = b & 0xFF; 
  return integer.tohexstring (i); /** * Converts a 4byte array to a 32-bit int * * @param buf * bytes buffer * @param byte[] where the conversion begins * @r 
    Eturn 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 << | secondbyte << | thirdbyte << 8 | fourthbyte)) & 0xFFFFFFFFL; /** * Converts a 16-bit short to a byte array * * @param s * short * @return byte[] Length 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; /** * Converts a 32-bit integer to a byte array of length 4 * * @param s * int * @return byte[] * */public static B 
    Yte[] 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 bit int to byte[]*/public static byte[] Int2byte (int res) {byte[] targets = new BYTE[4]; Targets[0] = (byte) (res & 0xff);//lowest bit targets[1] = (byte) ((res >> 8) & 0xff);/times Low targets[2] 
    = (byte) (res >>) & 0xff);//secondary high targets[3] = (byte) (res >>> 24);//highest bit, unsigned right shift. 
  return targets; /** * Converts a byte array with a length of 2 to a 16-bit int * * @param res * byte[] * @return int */public Stati 
    c int Byte2int (byte[] res) {/Res = Inversionbyte (res); One byte data left 24 digits to 0x?? 000000, and then move to the right 8 to become 0x00?? 0000 int targets = (Res[0] & 0xff) | ((Res[1] << 8) & 0xff00); // | Indicates an Targ or returnETs  } 
}

The above is a small series for everyone to bring Java byte array and int,long,short,byte conversion implementation of the whole content, I hope that we support cloud-Habitat Community ~

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.