[Java] Java byte array and int,long,short,byte conversion

Source: Internet
Author: User


  • public class Datatypechangehelper {
  • /**
  • * Converts a single byte of 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 binary
  • *
  • * @param b
  • * Byte
  • * @return Convert result
  • */
  • public static String Bytetohex (Byte b) {
  • int i = b & 0xFF;
  • return integer.tohexstring (i);
  • }
  • /**
  • * Convert an array of 4byte to 32-bit int
  • *
  • * @param buf
  • * Bytes Buffer
  • * Where to start converting @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]));
  • 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 is 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 = (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 byte[] Inttobytearray (int s) {
  • Byte[] targets = new BYTE[2];
  • for (int i = 0; i < 4; i++) {
  • int offset = (Targets.length-1-i) * 8;
  • Targets = (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 = (byte) ((S >>> offset) & 0xff);
  • }
  • return targets;
  • }
  • /**32 bit int turn 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);//Sub-low
  • TARGETS[2] = (byte) ((res >>) & 0xff);//Secondary high
  • TARGETS[3] = (byte) (res >>> 24);//highest bit, unsigned right shift.
  • return targets;
  • }
  • /**
  • * Converts a byte array of length 2 to 16-bit int
  • *
  • * @param Res
  • * byte[]
  • * @return int
  • * */
  • public static int Byte2int (byte[] res) {
  • res = Inversionbyte (res);
  • A byte data shifted left 24 bits into 0x?? 000000, then move right 8 bits into 0x00?? 0000
  • int targets = (Res[0] & 0xff) | ((Res[1] << 8) & 0xff00); // | Represents an security position or
  • return targets;
  • }
  • }


[Java] Java byte array and int,long,short,byte conversion

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.