Java之——基本数据类型与byte数组相互转化

本文介绍了一个Java工具类ByteUtil,该类提供了基本数据类型与byte数组之间的转换方法,包括short、char、int、long、float、double及String等类型的转换实现。
  1. package cn.com.eteamsun.utils;  
  2.   
  3. import java.nio.charset.Charset;  
  4.   
  5. /** 
  6.  * Java基本数据类型和byte数组相互转化 
  7.  * @author liuyazhuang 
  8.  * 
  9.  */  
  10. public class ByteUtil {  
  11.     public static byte[] getBytes(short data) {  
  12.         byte[] bytes = new byte[2];  
  13.         bytes[0] = (byte) (data & 0xff);  
  14.         bytes[1] = (byte) ((data & 0xff00) >> 8);  
  15.         return bytes;  
  16.     }  
  17.   
  18.     public static byte[] getBytes(char data) {  
  19.         byte[] bytes = new byte[2];  
  20.         bytes[0] = (byte) (data);  
  21.         bytes[1] = (byte) (data >> 8);  
  22.         return bytes;  
  23.     }  
  24.   
  25.     public static byte[] getBytes(int data) {  
  26.         byte[] bytes = new byte[4];  
  27.         bytes[0] = (byte) (data & 0xff);  
  28.         bytes[1] = (byte) ((data & 0xff00) >> 8);  
  29.         bytes[2] = (byte) ((data & 0xff0000) >> 16);  
  30.         bytes[3] = (byte) ((data & 0xff000000) >> 24);  
  31.         return bytes;  
  32.     }  
  33.   
  34.     public static byte[] getBytes(long data) {  
  35.         byte[] bytes = new byte[8];  
  36.         bytes[0] = (byte) (data & 0xff);  
  37.         bytes[1] = (byte) ((data >> 8) & 0xff);  
  38.         bytes[2] = (byte) ((data >> 16) & 0xff);  
  39.         bytes[3] = (byte) ((data >> 24) & 0xff);  
  40.         bytes[4] = (byte) ((data >> 32) & 0xff);  
  41.         bytes[5] = (byte) ((data >> 40) & 0xff);  
  42.         bytes[6] = (byte) ((data >> 48) & 0xff);  
  43.         bytes[7] = (byte) ((data >> 56) & 0xff);  
  44.         return bytes;  
  45.     }  
  46.   
  47.     public static byte[] getBytes(float data) {  
  48.         int intBits = Float.floatToIntBits(data);  
  49.         return getBytes(intBits);  
  50.     }  
  51.   
  52.     public static byte[] getBytes(double data) {  
  53.         long intBits = Double.doubleToLongBits(data);  
  54.         return getBytes(intBits);  
  55.     }  
  56.   
  57.     public static byte[] getBytes(String data, String charsetName) {  
  58.         Charset charset = Charset.forName(charsetName);  
  59.         return data.getBytes(charset);  
  60.     }  
  61.   
  62.     public static byte[] getBytes(String data) {  
  63.         return getBytes(data, "GBK");  
  64.     }  
  65.   
  66.     public static short getShort(byte[] bytes) {  
  67.         return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));  
  68.     }  
  69.   
  70.     public static char getChar(byte[] bytes) {  
  71.         return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));  
  72.     }  
  73.   
  74.     public static int getInt(byte[] bytes) {  
  75.         return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16))  
  76.                 | (0xff000000 & (bytes[3] << 24));  
  77.     }  
  78.   
  79.     public static long getLong(byte[] bytes) {  
  80.         return (0xffL & (long) bytes[0]) | (0xff00L & ((long) bytes[1] << 8)) | (0xff0000L & ((long) bytes[2] << 16))  
  81.                 | (0xff000000L & ((long) bytes[3] << 24)) | (0xff00000000L & ((long) bytes[4] << 32))  
  82.                 | (0xff0000000000L & ((long) bytes[5] << 40)) | (0xff000000000000L & ((long) bytes[6] << 48))  
  83.                 | (0xff00000000000000L & ((long) bytes[7] << 56));  
  84.     }  
  85.   
  86.     public static float getFloat(byte[] bytes) {  
  87.         return Float.intBitsToFloat(getInt(bytes));  
  88.     }  
  89.   
  90.     public static double getDouble(byte[] bytes) {  
  91.         long l = getLong(bytes);  
  92.         System.out.println(l);  
  93.         return Double.longBitsToDouble(l);  
  94.     }  
  95.   
  96.     public static String getString(byte[] bytes, String charsetName) {  
  97.         return new String(bytes, Charset.forName(charsetName));  
  98.     }  
  99.   
  100.     public static String getString(byte[] bytes) {  
  101.         return getString(bytes, "GBK");  
  102.     }  
  103.   
  104.     public static void main(String[] args) {  
  105.         short s = 122;  
  106.         int i = 122;  
  107.         long l = 1222222;  
  108.   
  109.         char c = 'a';  
  110.   
  111.         float f = 122.22f;  
  112.         double d = 122.22;  
  113.   
  114.         String string = "我是好孩子";  
  115.         System.out.println(s);  
  116.         System.out.println(i);  
  117.         System.out.println(l);  
  118.         System.out.println(c);  
  119.         System.out.println(f);  
  120.         System.out.println(d);  
  121.         System.out.println(string);  
  122.   
  123.         System.out.println("**************");  
  124.   
  125.         System.out.println(getShort(getBytes(s)));  
  126.         System.out.println(getInt(getBytes(i)));  
  127.         System.out.println(getLong(getBytes(l)));  
  128.         System.out.println(getChar(getBytes(c)));  
  129.         System.out.println(getFloat(getBytes(f)));  
  130.         System.out.println(getDouble(getBytes(d)));  
  131.         System.out.println(getString(getBytes(string)));  
  132.     }  
  133. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值