byte[]与各种数据类型互相转换示例

在socket开发过程中,通常需要将一些具体的值(这些值可能是各种JAVA类型)转化为byte[]类型,为此我总结了如下这个示例,贴出来,以便经常翻看

 

Java代码 复制代码  收藏代码byte[]与各种数据类型互相转换示例
  1. public class TestCase {   
  2.        
  3.       
  4.     public static byte[] shortToByte(short number) {   
  5.         int temp number;   
  6.         byte[] new byte[2];   
  7.         for (int 0; b.length; i++) {   
  8.             b[i] new Integer(temp 0xff).byteValue();// 将最低位保存在最低位   
  9.             temp temp >> 8;// 向右移8位   
  10.         }   
  11.         return b;   
  12.     }   
  13.   
  14.       
  15.     public static short byteToShort(byte[] b) {   
  16.         short 0;   
  17.         short s0 (short) (b[0] 0xff);// 最低位   
  18.         short s1 (short) (b[1] 0xff);   
  19.         s1 <<= 8;   
  20.         (short) (s0 s1);   
  21.         return s;   
  22.     }   
  23.        
  24.        
  25.       
  26.     public static byte[] intToByte(int number) {   
  27.         int temp number;   
  28.         byte[] new byte[4];   
  29.         for (int 0; b.length; i++) {   
  30.             b[i] new Integer(temp 0xff).byteValue();// 将最低位保存在最低位   
  31.             temp temp >> 8;// 向右移8位   
  32.         }   
  33.         return b;   
  34.     }   
  35.   
  36.       
  37.     public static int byteToInt(byte[] b) {   
  38.         int 0;   
  39.         int s0 b[0] 0xff;// 最低位   
  40.         int s1 b[1] 0xff;   
  41.         int s2 b[2] 0xff;   
  42.         int s3 b[3] 0xff;   
  43.         s3 <<= 24;   
  44.         s2 <<= 16;   
  45.         s1 <<= 8;   
  46.         s0 s1 s2 s3;   
  47.         return s;   
  48.     }   
  49.        
  50.        
  51.       
  52.     public static byte[] longToByte(long number) {   
  53.         long temp number;   
  54.         byte[] new byte[8];   
  55.         for (int 0; b.length; i++) {   
  56.             b[i] new Long(temp 0xff).byteValue();// 将最低位保存在最低位 temp temp   
  57.                                                         // >> 8;// 向右移8位   
  58.         }   
  59.         return b;   
  60.     }   
  61.   
  62.       
  63.     public static long byteToLong(byte[] b) {   
  64.         long 0;   
  65.         long s0 b[0] 0xff;// 最低位   
  66.         long s1 b[1] 0xff;   
  67.         long s2 b[2] 0xff;   
  68.         long s3 b[3] 0xff;   
  69.         long s4 b[4] 0xff;// 最低位   
  70.         long s5 b[5] 0xff;   
  71.         long s6 b[6] 0xff;   
  72.         long s7 b[7] 0xff;   
  73.   
  74.         // s0不变   
  75.         s1 <<= 8;   
  76.         s2 <<= 16;   
  77.         s3 <<= 24;   
  78.         s4 <<= 4;   
  79.         s5 <<= 5;   
  80.         s6 <<= 6;   
  81.         s7 <<= 7;   
  82.         s0 s1 s2 s3 s4 s5 s6 s7;   
  83.         return s;   
  84.     }   
  85.        
  86.       
  87.     public static byte[] doubleToByte(double num)     
  88.         byte[] new byte[8];     
  89.         long Double.doubleToLongBits(num);     
  90.         for (int 0; 8; i++)     
  91.             b[i] new Long(l).byteValue();     
  92.             >> 8;     
  93.         }   
  94.         return b;   
  95.     }   
  96.        
  97.       
  98.     public static double getDouble(byte[] b)     
  99.         long m;     
  100.         b[0];     
  101.         &= 0xff;     
  102.         |= ((long) b[1] << 8);     
  103.         &= 0xffff;     
  104.         |= ((long) b[2] << 16);     
  105.         &= 0xffffff;     
  106.         |= ((long) b[3] << 24);     
  107.         &= 0xffffffffl;     
  108.         |= ((long) b[4] << 32);     
  109.         &= 0xffffffffffl;     
  110.         |= ((long) b[5] << 40);     
  111.         &= 0xffffffffffffl;     
  112.         |= ((long) b[6] << 48);     
  113.         &= 0xffffffffffffffl;     
  114.         |= ((long) b[7] << 56);     
  115.         return Double.longBitsToDouble(m);     
  116.     }   
  117.        
  118.        
  119.       
  120.     public static void floatToByte(float x) {   
  121.         //先用 Float.floatToIntBits(f)转换成int   
  122.     }   
  123.        
  124.       
  125.     public static float getFloat(byte[] b)     
  126.         // bytes     
  127.         int accum 0;     
  128.         for int shiftBy 0; shiftBy 4; shiftBy++     
  129.                 accum |= (b[shiftBy] 0xff) << shiftBy 8;     
  130.             
  131.         return Float.intBitsToFloat(accum);     
  132.         
  133.   
  134.        
  135.      public static byte[] charToByte(char c){   
  136.         byte[] new byte[2];   
  137.         b[0] (byte) ((c 0xFF00) >> 8);   
  138.         b[1] (byte) (c 0xFF);   
  139.         return b;   
  140.      }   
  141.         
  142.        
  143.      public static char byteToChar(byte[] b){   
  144.         char (char) (((b[0] 0xFF) << 8) (b[1] 0xFF));   
  145.         return c;   
  146.      }   
  147.        
  148.       
  149.     public static byte[] stringToByte(String str) throws UnsupportedEncodingException{   
  150.         return str.getBytes("GBK");   
  151.     }   
  152.        
  153.       
  154.     public static String bytesToString(byte[] str) {   
  155.         String keyword null;   
  156.         try {   
  157.             keyword new String(str,"GBK");   
  158.         catch (UnsupportedEncodingException e) {   
  159.             e.printStackTrace();   
  160.         }   
  161.         return keyword;   
  162.     }   
  163.        
  164.        
  165.       
  166.     @Test  
  167.     public void testObject2ByteArray() throws IOException,   
  168.             ClassNotFoundException {   
  169.         // Object obj "";   
  170.         Integer[] obj 1, 3, };   
  171.   
  172.         // // object to bytearray   
  173.         ByteArrayOutputStream bo new ByteArrayOutputStream();   
  174.         ObjectOutputStream oo new ObjectOutputStream(bo);   
  175.         oo.writeObject(obj);   
  176.         byte[] bytes bo.toByteArray();   
  177.         bo.close();   
  178.         oo.close();   
  179.         System.out.println(Arrays.toString(bytes));   
  180.   
  181.         Integer[] intArr (Integer[]) testByteArray2Object(bytes);   
  182.         System.out.println(Arrays.asList(intArr));   
  183.   
  184.   
  185.         byte[] b2 intToByte(123);   
  186.         System.out.println(Arrays.toString(b2));   
  187.   
  188.         int byteToInt(b2);   
  189.         System.out.println(a);   
  190.   
  191.     }   
  192.   
  193.       
  194.     private Object testByteArray2Object(byte[] bytes) throws IOException,   
  195.             ClassNotFoundException {   
  196.         // byte[] bytes null;   
  197.         Object obj;   
  198.         // bytearray to object   
  199.         ByteArrayInputStream bi new ByteArrayInputStream(bytes);   
  200.         ObjectInputStream oi new ObjectInputStream(bi);   
  201.         obj oi.readObject();   
  202.         bi.close();   
  203.         oi.close();   
  204.         System.out.println(obj);   
  205.         return obj;   
  206.     }   
  207.   
  208. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值