Java基本类型与byte数组之间相互转换

学习笔记,转自http://blog.sina.com.cn/s/blog_7a35101201012n0b.html

  1 import java.nio.charset.Charset;
  2 
  3 public class ByteUtil
  4 {
  5     public static byte[] getBytes(short data)
  6     {
  7         byte[] bytes = new byte[2];
  8         bytes[0] = (byte) (data & 0xff);
  9         bytes[1] = (byte) ((data & 0xff00) >> 8);
 10         return bytes;
 11     }
 12 
 13     public static byte[] getBytes(char data)
 14     {
 15         byte[] bytes = new byte[2];
 16         bytes[0] = (byte) (data);
 17         bytes[1] = (byte) (data >> 8);
 18         return bytes;
 19     }
 20 
 21     public static byte[] getBytes(int data)
 22     {
 23         byte[] bytes = new byte[4];
 24         bytes[0] = (byte) (data & 0xff);
 25         bytes[1] = (byte) ((data & 0xff00) >> 8);
 26         bytes[2] = (byte) ((data & 0xff0000) >> 16);
 27         bytes[3] = (byte) ((data & 0xff000000) >> 24);
 28         return bytes;
 29     }
 30 
 31     public static byte[] getBytes(long data)
 32     {
 33         byte[] bytes = new byte[8];
 34         bytes[0] = (byte) (data & 0xff);
 35         bytes[1] = (byte) ((data >> 8) & 0xff);
 36         bytes[2] = (byte) ((data >> 16) & 0xff);
 37         bytes[3] = (byte) ((data >> 24) & 0xff);
 38         bytes[4] = (byte) ((data >> 32) & 0xff);
 39         bytes[5] = (byte) ((data >> 40) & 0xff);
 40         bytes[6] = (byte) ((data >> 48) & 0xff);
 41         bytes[7] = (byte) ((data >> 56) & 0xff);
 42         return bytes;
 43     }
 44 
 45     public static byte[] getBytes(float data)
 46     {
 47         int intBits = Float.floatToIntBits(data);
 48         return getBytes(intBits);
 49     }
 50 
 51     public static byte[] getBytes(double data)
 52     {
 53         long intBits = Double.doubleToLongBits(data);
 54         return getBytes(intBits);
 55     }
 56 
 57     public static byte[] getBytes(String data, String charsetName)
 58     {
 59         Charset charset = Charset.forName(charsetName);
 60         return data.getBytes(charset);
 61     }
 62 
 63     public static byte[] getBytes(String data)
 64     {
 65         return getBytes(data, "GBK");
 66     }
 67 
 68     
 69     public static short getShort(byte[] bytes)
 70     {
 71         return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
 72     }
 73 
 74     public static char getChar(byte[] bytes)
 75     {
 76         return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
 77     }
 78 
 79     public static int getInt(byte[] bytes)
 80     {
 81         return (0xff & bytes[0]) | 
 82         (0xff00 & (bytes[1] << 8)) | 
 83         (0xff0000 & (bytes[2] << 16)) | 
 84         (0xff000000 & (bytes[3] << 24));
 85     }
 86    
 87     public static long getLong(byte[] bytes)
 88     {
 89         return(0xffL & (long)bytes[0]) | 
 90         (0xff00L & ((long)bytes[1] << 8)) | 
 91         (0xff0000L & ((long)bytes[2] << 16)) |
 92         (0xff000000L & ((long)bytes[3] << 24)) | 
 93         (0xff00000000L & ((long)bytes[4] << 32)) | 
 94         (0xff0000000000L & ((long)bytes[5] << 40)) | 
 95         (0xff000000000000L & ((long)bytes[6] << 48)) | 
 96         (0xff00000000000000L & ((long)bytes[7] << 56));
 97     }
 98 
 99     public static float getFloat(byte[] bytes)
100     {
101         return Float.intBitsToFloat(getInt(bytes));
102     }
103 
104     public static double getDouble(byte[] bytes)
105     {
106         long l = getLong(bytes);
107         //System.out.println(l);
108         return Double.longBitsToDouble(l);
109     }
110 
111     public static String getString(byte[] bytes, String charsetName)
112     {
113         return new String(bytes, Charset.forName(charsetName));
114     }
115 
116     public static String getString(byte[] bytes)
117     {
118         return getString(bytes, "GBK");
119     }
120 
121     
122     public static void main(String[] args)
123     {
124         short s = 122;
125         int i = 122;
126         long l = 1222222;
127 
128         char c = 'a';
129 
130         float f = 122.22f;
131         double d = 122.22;
132 
133         String string = "我是好孩子";
134         System.out.println(s);
135         System.out.println(i);
136         System.out.println(l);
137         System.out.println(c);
138         System.out.println(f);
139         System.out.println(d);
140         System.out.println(string);
141 
142         System.out.println("**************");
143 
144         System.out.println(getShort(getBytes(s)));
145         System.out.println(getInt(getBytes(i)));
146         System.out.println(getLong(getBytes(l)));
147         System.out.println(getChar(getBytes(c)));
148         System.out.println(getFloat(getBytes(f)));
149         //System.out.println(getDouble(getBytes(d)));
150         System.out.println(getString(getBytes(string)));
151     }
152 }
原文地址:https://www.cnblogs.com/ning1121/p/3482959.html