The Java base type is converted to and from a byte array, which is just written
Package cn.teaey.utils;
Import Java.nio.charset.Charset;
public class Byteutil {public static byte[] GetBytes (short data) {byte[] bytes = new Byte[2];
Bytes[0] = (byte) (Data & 0xff);
BYTES[1] = (byte) (Data & 0xff00) >> 8);
return bytes;
public static byte[] GetBytes (char data) {byte[] bytes = new Byte[2];
Bytes[0] = (byte) (data);
BYTES[1] = (byte) (data >> 8);
return bytes;
public static byte[] getBytes (int data) {byte[] bytes = new Byte[4];
Bytes[0] = (byte) (Data & 0xff);
BYTES[1] = (byte) (Data & 0xff00) >> 8);
BYTES[2] = (byte) (Data & 0xff0000) >> 16);
BYTES[3] = (byte) (Data & 0xff000000) >> 24);
return bytes;
public static byte[] GetBytes (Long data) {byte[] bytes = new Byte[8];
Bytes[0] = (byte) (Data & 0xff);
BYTES[1] = (byte) (Data >> 8) & 0xff); BYTES[2] = (byte) (Data >>) &0xFF);
BYTES[3] = (byte) (Data >>) & 0xff);
BYTES[4] = (byte) (Data >>) & 0xff);
BYTES[5] = (byte) (Data >>) & 0xff);
BYTES[6] = (byte) (Data >>) & 0xff);
BYTES[7] = (byte) (Data >>) & 0xff);
return bytes;
public static byte[] GetBytes (float data) {int intbits = float.floattointbits (data);
Return GetBytes (intbits);
public static byte[] GetBytes (double data) {Long intbits = double.doubletolongbits (data);
Return GetBytes (intbits); public static byte[] GetBytes (string data, String charsetname) {Charset Charset = Charset.forname (charsetname
);
Return Data.getbytes (CharSet);
public static byte[] GetBytes (String data) {return getBytes (data, "GBK"); public static short Getshort (byte[] bytes) {return (short) (0xFF & Bytes[0]) |
(0xff00 & (Bytes[1] << 8)); public static char GetChar (byte[] BYtes) {return (char) (0xFF & Bytes[0]) |
(0xff00 & (Bytes[1] << 8)); public static int getInt (byte[] bytes) {return (0xFF & Bytes[0]) | (0xff00 & (Bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) |
(0xFF000000 & (Bytes[3] << 24)); public static long Getlong (byte[] bytes) {return (0xffL & (Long) bytes[0]) | (0xff00l & ((Long) bytes[1] << 8) | (0xff0000l & ((Long) bytes[2] << 16) | (0xff000000l & ((Long) bytes[3] << 24) | (0xff00000000l & ((Long) bytes[4] << 32) | (0xff0000000000l & ((Long) bytes[5] << 40) | (0xff000000000000l & ((Long) bytes[6] << 48) |
(0xff00000000000000l & ((Long) bytes[7] << 56));
public static float getfloat (byte[] bytes) {return float.intbitstofloat (getInt (bytes));
public static double getdouble (byte[] bytes) {Long L = getlong (bytes);
System.out.println (l); return doUble.longbitstodouble (l); public static string getString (byte[] bytes, String charsetname) {return new string (bytes, charset.forname (ch
Arsetname));
public static String getString (byte[] bytes) {return getString (bytes, "GBK");
public static void Main (string[] args) {short s = 122;
int i = 122;
Long L = 1222222;
char c = ' a ';
float F = 122.22f;
Double d = 122.22;
String string = "I am a good child";
System.out.println (s);
System.out.println (i);
System.out.println (l);
System.out.println (c);
System.out.println (f);
System.out.println (d);
System.out.println (string);
System.out.println ("**************");
System.out.println (Getshort (GetBytes (s)));
System.out.println (GetInt (GetBytes (i)));
System.out.println (Getlong (GetBytes (l)));
System.out.println (GetChar (GetBytes (c)));
System.out.println (GetFloat (GetBytes (f)));
System.out.println (GetDouble (getBytes (d))); System.out. println (GetString (GetBytes (string)); }
}
The above Java basic type and byte array of the conversion method is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.