Java byte array and INT, long, short, byte Conversion

Source: Internet
Author: User

 

Public class datatypechangehelper {/*** converts a single-byte to a 32-bit int ** @ Param B * byte * @ return convert result */public static int unsignedbytetoint (byte b) {return (INT) B & 0xff ;} /*** convert a single byte to a hexadecimal number ** @ Param B * byte * @ return convert result */public static string bytetohex (byte B) {int I = B & 0xff; return integer. tohexstring (I);}/*** converts a 4-byte array to a 32-bit int ** @ Param Buf * bytes buffer *@ The conversion starting position in 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]); ind Ex = index + 4; Return (long) (firstbyte <24 | secondbyte <16 | thirdbyte <8 | fourthbyte) & 0 xffffffffl ;} /*** convert a 16-bit short to a byte array. ** @ Param S * short * @ return byte [] the length is 2 **/public static byte [] bytes tobytearray (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 ;}/*** Convert a 32-bit integer to a 4-byte array ** @ 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-bit int to byte [] */public static byte [] int2byte (INT res) {byte [] targets = new byte [4]; targets [0] = (byte) (Res & 0xff); // second bit targets [1] = (byte) (RES> 8) & 0xff ); // low position targets [2] = (byte) (RES> 16) & 0xff); // high position targets [3] = (byte) (RES> 24); // The highest bit, shifts right unsigned. Return targets ;} /*** convert the byte array with a length of 2 to a 16-bit int ** @ Param res * byte [] * @ return int **/public static int byte2int (byte [] res) {// res = inversionbyte (RES); // shifts the value of a byte to a value of 0x ?? 000000, then shift the eight digits to 0x00 ?? Optional int targets = (RES [0] & 0xff) | (RES [1] <8) & 0xff00); // | indicates an AMAP or return targets ;}}

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.