基本数据类型和byte型之间的转换
我们经常用到数据类型之间的转换,下面这个工具类是基本数据类型和byte型之间的转换。
import java.nio.charset.Charset; public class ByteUtil { /** * short型转换成byte型 * @param data * @return */ public static byte[] shortToBytes(short data) { byte[] bytes = new byte[2]; bytes[0] = (byte) (data & 0xff); bytes[1] = (byte) ((data & 0xff00) >> 8); return bytes; } /** * char型转换成byte型 * @param data * @return */ public static byte[] charToBytes(char data) { byte[] bytes = new byte[2]; bytes[0] = (byte) (data); bytes[1] = (byte) (data >> 8); return bytes; } /** * int型转换成byte型 * @param data * @return */ public static byte[] intToBytes(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; } /** * long型转换成byte型 * @param data * @return */ public static byte[] longToBytes(long data) { byte[] bytes = new byte[8]; bytes[0] = (byte) (data & 0xff); bytes[1] = (byte) ((data >> 8) & 0xff); bytes[2] = (byte) ((data >> 16) & 0xff); bytes[3] = (byte) ((data >> 24) & 0xff); bytes[4] = (byte) ((data >> 32) & 0xff); bytes[5] = (byte) ((data >> 40) & 0xff); bytes[6] = (byte) ((data >> 48) & 0xff); bytes[7] = (byte) ((data >> 56) & 0xff); return bytes; } /** * float型转换成byte型 * @param data * @return */ public static byte[] floatToBytes(float data) { int intBits = Float.floatToIntBits(data); return intToBytes(intBits); } /** * double型转换成byte型 * @param data * @return */ public static byte[] doubleToBytes(double data) { long intBits = Double.doubleToLongBits(data); return longToBytes(intBits); } /** * String型转换成byte型 * @param data * @param charsetName * @return */ public static byte[] stringToBytes(String data, String charsetName) { Charset charset = Charset.forName(charsetName); return data.getBytes(charset); } /** * String型转换成byte型 * @param data * @return */ public static byte[] stringToBytes(String data) { return stringToBytes(data, "GBK"); } /** * byte型转换成short型 * @param bytes * @return */ public static short bytesToShort(byte[] bytes) { return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8))); } /** * byte型转换成char型 * @param bytes * @return */ public static char bytesToChar(byte[] bytes) { return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8))); } /** * byte型转换成int型 * @param bytes * @return */ public static int bytesToInt(byte[] bytes) { return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24)); } /** * byte型转换成long型 * @param bytes * @return */ public static long bytesToLong(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)); } /** * byte型转换成float型 * @param bytes * @return */ public static float bytesToFloat(byte[] bytes) { return Float.intBitsToFloat(bytesToInt(bytes)); } /** * byte型转换成double型 * @param bytes * @return */ public static double bytesToDouble(byte[] bytes) { long l = bytesToLong(bytes); return Double.longBitsToDouble(l); } /** * byte型转换成String型 * @param bytes * @param charsetName * @return */ public static String bytesToString(byte[] bytes, String charsetName) { return new String(bytes, Charset.forName(charsetName)); } /** * byte型转换成String型 * @param bytes * @return */ public static String bytesToString(byte[] bytes) { return bytesToString(bytes, "GBK"); } }
写一个demo调用上面的工具类:
public class Test { /** * @param args */ 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 = "你好JAVA!";
System.out.println("s: " + s); System.out.println("i: " + i); System.out.println("l: " + l); System.out.println("c: " + c); System.out.println("f: " + f); System.out.println("d: " + d); System.out.println("string: " + string); System.out.println("********调用转换后********"); System.out.println("s: " + ByteUtil.bytesToShort(ByteUtil.shortToBytes(s))); System.out.println("i: " + ByteUtil.bytesToInt(ByteUtil.intToBytes(i))); System.out.println("l: " + ByteUtil.bytesToLong(ByteUtil.longToBytes(l))); System.out.println("c: " + ByteUtil.bytesToChar(ByteUtil.charToBytes(c))); System.out.println("f: " + ByteUtil.bytesToFloat((ByteUtil.floatToBytes(f)))); System.out.println("d: " + ByteUtil.bytesToDouble(ByteUtil.doubleToBytes(d))); System.out.println("string: " + ByteUtil.bytesToString(ByteUtil.stringToBytes(string))); } }
输出结果:
s: 122
i: 122
l: 1222222
c: a
f: 122.22
d: 122.22
string: 你好JAVA!
********调用转换后********
s: 122
i: 122
l: 1222222
c: a
f: 122.22
d: 122.22
string: 你好JAVA!