Conversion between byte, hexadecimal and Basic Types

Source: Internet
Author: User

Java's API source code contains some processing methods for bytes. The following code is used to copy or paste the modified Code.

// Convert byte [] to int. Off indicates the start position of byte. Static int getint (byte [] B, int off) {return (B [off + 3] & 0xff) <0) + (B [off + 2] & 0xff) <8) + (B [off + 1] & 0xff) <16) + (B [off + 0]) <24 );} // convert int to byte [] method static void putint (byte [] B, int off, int Val) {B [off + 3] = (byte) (Val >>> 0); B [off + 2] = (byte) (Val >>> 8); B [off + 1] = (byte) (Val >>> 16); B [off + 0] = (byte) (Val >>> 24 );}

The above is an example. The method comes from the bits. Class file.
All the methods for this file are as follows.

    /*     * Methods for unpacking primitive values from byte arrays starting at     * given offsets.     */    static boolean getBoolean(byte[] b, int off) {return b[off] != 0;    }        static char getChar(byte[] b, int off) {return (char) (((b[off + 1] & 0xFF) << 0) +        ((b[off + 0]) << 8));    }        static short getShort(byte[] b, int off) {return (short) (((b[off + 1] & 0xFF) << 0) + ((b[off + 0]) << 8));    }        static int getInt(byte[] b, int off) {return ((b[off + 3] & 0xFF) << 0) +       ((b[off + 2] & 0xFF) << 8) +       ((b[off + 1] & 0xFF) << 16) +       ((b[off + 0]) << 24);    }        static float getFloat(byte[] b, int off) {int i = ((b[off + 3] & 0xFF) << 0) +((b[off + 2] & 0xFF) << 8) +((b[off + 1] & 0xFF) << 16) +((b[off + 0]) << 24);return Float.intBitsToFloat(i);    }        static long getLong(byte[] b, int off) {return ((b[off + 7] & 0xFFL) << 0) +       ((b[off + 6] & 0xFFL) << 8) +       ((b[off + 5] & 0xFFL) << 16) +       ((b[off + 4] & 0xFFL) << 24) +       ((b[off + 3] & 0xFFL) << 32) +       ((b[off + 2] & 0xFFL) << 40) +       ((b[off + 1] & 0xFFL) << 48) +       (((long) b[off + 0]) << 56);    }    static double getDouble(byte[] b, int off) {long j = ((b[off + 7] & 0xFFL) << 0) + ((b[off + 6] & 0xFFL) << 8) + ((b[off + 5] & 0xFFL) << 16) + ((b[off + 4] & 0xFFL) << 24) + ((b[off + 3] & 0xFFL) << 32) + ((b[off + 2] & 0xFFL) << 40) + ((b[off + 1] & 0xFFL) << 48) + (((long) b[off + 0]) << 56);return Double.longBitsToDouble(j);    }        /*     * Methods for packing primitive values into byte arrays starting at given     * offsets.     */    static void putBoolean(byte[] b, int off, boolean val) {b[off] = (byte) (val ? 1 : 0);    }    static void putChar(byte[] b, int off, char val) {b[off + 1] = (byte) (val >>> 0);b[off + 0] = (byte) (val >>> 8);    }    static void putShort(byte[] b, int off, short val) {b[off + 1] = (byte) (val >>> 0);b[off + 0] = (byte) (val >>> 8);    }    static void putInt(byte[] b, int off, int val) {b[off + 3] = (byte) (val >>> 0);b[off + 2] = (byte) (val >>> 8);b[off + 1] = (byte) (val >>> 16);b[off + 0] = (byte) (val >>> 24);    }    static void putFloat(byte[] b, int off, float val) {int i = Float.floatToIntBits(val);b[off + 3] = (byte) (i >>> 0);b[off + 2] = (byte) (i >>> 8);b[off + 1] = (byte) (i >>> 16);b[off + 0] = (byte) (i >>> 24);    }    static void putLong(byte[] b, int off, long val) {b[off + 7] = (byte) (val >>> 0);b[off + 6] = (byte) (val >>> 8);b[off + 5] = (byte) (val >>> 16);b[off + 4] = (byte) (val >>> 24);b[off + 3] = (byte) (val >>> 32);b[off + 2] = (byte) (val >>> 40);b[off + 1] = (byte) (val >>> 48);b[off + 0] = (byte) (val >>> 56);    }    static void putDouble(byte[] b, int off, double val) {long j = Double.doubleToLongBits(val);b[off + 7] = (byte) (j >>> 0);b[off + 6] = (byte) (j >>> 8);b[off + 5] = (byte) (j >>> 16);b[off + 4] = (byte) (j >>> 24);b[off + 3] = (byte) (j >>> 32);b[off + 2] = (byte) (j >>> 40);b[off + 1] = (byte) (j >>> 48);b[off + 0] = (byte) (j >>> 56);    }

This method is also available in datainputstream.

// Modify the public static int toint (byte [] B, int off) from datainputstream throws eofexception {int outer = B [off + 0]; int CH2 = B [off + 1]; int CH3 = B [off + 2]; int methane = B [off + 3]; if (distinct | CH2 | CH3 | methane) <0) throw new eofexception (); Return (distinct <24) + (CH2 <16) + (CH3 <8) + (methane <0 ));}

However, the source code of this class only includes methods of converting to the basic type, which is similar to the methods in the bits. Class file.
The system converts the basic type to a byte with a low storage level.
For example, the short type has 16 bits, the first byte stores 8 bits, and the second byte stores 8 bits.

The following method comes from the network and does not fully verify its correctness. Note: Some of the methods for converting bytes are to save the low position in the low position.

// Convert the long type to a byte array

Public static byte [] longtobyte (long number ){
Long temp = number;
Byte [] B = new byte [8];
For (INT I = 0; I <B. length; I ++ ){
B [I] = new long (temp & 0xff). bytevalue (); // Save the second bit in the second bit
Temp = temp> 8; // shift 8 digits to the right
}
Return B;
}

// Convert byte array to long
Public static long bytetolong (byte [] B ){
Long S = 0;
Long S0 = B [0] & 0xff; // second bit
Long S1 = B [1] & 0xff;
Long S2 = B [2] & 0xff;
Long S3 = B [3] & 0xff;
Long S4 = B [4] & 0xff; // second bit
Long S5 = B [5] & 0xff;
Long S6 = B [6] & 0xff;
Long S7 = B [7] & 0xff;
 
// S0 unchanged
S1 <= 8;
S2 <= 16;
S3 <= 24;
S4 <= 8*4;
S5 <= 8*5;
S6 <= 8*6;
S7 <= 8*7;
S = S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7;
Return S;
}
 
/**
* Note: int to byte array conversion!
*
* @ Param number
* @ Return
*/
Public static byte [] inttobyte (INT number ){
Int temp = number;
Byte [] B = new byte [4];
For (INT I = 0; I <B. length; I ++ ){
B [I] = new INTEGER (temp & 0xff). bytevalue (); // Save the second bit in the second bit
Temp = temp> 8; // shift 8 digits to the right
}
Return B;
}
 
/**
* Note: conversion from byte array to int!
*
* @ Param B
* @ Return
*/
Public static int bytetoint (byte [] B ){
Int S = 0;
Int S0 = B [0] & 0xff; // second bit
Int S1 = B [1] & 0xff;
Int S2 = B [2] & 0xff;
Int S3 = B [3] & 0xff;
S3 <= 24;
S2 <= 16;
S1 <= 8;
S = S0 | S1 | S2 | S3;
Return S;
}
 
/**
* Note: short to byte array conversion!
*
* @ Param s
* @ Return
*/
Public static byte [] bytes tobyte (short number ){
Int temp = number;
Byte [] B = new byte [2];
For (INT I = 0; I <B. length; I ++ ){
B [I] = new INTEGER (temp & 0xff). bytevalue (); // Save the second bit in the second bit
Temp = temp> 8; // shift 8 digits to the right
}
Return B;
}
 
/**
* Note: conversion from byte array to short!
*
* @ Param B
* @ Return
*/
Public static short bytetoshort (byte [] B ){
Short S = 0;
Short S0 = (short) (B [0] & 0xff); // second bit
Short S1 = (short) (B [1] & 0xff );
S1 <= 8;
S = (short) (S0 | S1 );
Return S;

}

// Convert byte to hexadecimal public static final string tohex (byte B) {// 32 shifted to four places and then 28 characters in height are cleared, the result is the fourth to eighth digits of the 32-bit pair //.... 0000001111 & B Return ("" + "0123456789 abcdef ". charat (0xf & B> 4) + "0123456789 abcdef ". charat (B & 0xf);} // convert the hexadecimal string to byte [] public static byte [] hexstringtobytes (string hexstring) {If (hexstring = NULL | hexstring. equals ("") {return NULL;} hexstring = hexstring. touppercase (); int length = hexstring. length ()/2; char [] hexchars = hexstring. tochararray (); byte [] d = new byte [length]; for (INT I = 0; I <length; I ++) {int Pos = I * 2; d [I] = (byte) (chartobyte (hexchars [POS]) <4 | chartobyte (hexchars [POS + 1]);} return D ;} /*** convert Char to byte * @ Param C char * @ return byte */Private Static byte chartobyte (char c) {return (byte) "0123456789 abcdef ". indexof (c); // returns the decimal number of characters in hexadecimal notation, with 1 byte equal to 8-bit ASCII} // converts byte [] to hexadecimal notation. public static string bytes2hexstring (byte [] B) {string ret = ""; for (INT I = 0; I <B. length; I ++) {string hex = integer. tohexstring (B [I] & 0xff); If (hex. length () = 1) {hex = '0' + hex;} RET + = hex. touppercase ();} return ret ;}

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.