zoukankan      html  css  js  c++  java
  • 【转】java中float与byte[]的互转 -- 不错

    原文网址:http://tjmljw.iteye.com/blog/1767716

    起因:想把一个float[]转换成内存数据,查了一下,下面两个方法可以将float转成byte[]。 

    方法一 

    Java代码  收藏代码
    1. import java.nio.ByteBuffer;  
    2. import java.util.ArrayList;  
    3.   
    4. float buffer = 0f;  
    5. ByteBuffer bbuf = ByteBuffer.allocate(4);  
    6. bbuf.putFloat(buffer);  
    7. byte[] bBuffer = bbuf.array();  
    8. bBuffer=this.dataValueRollback(bBuffer);  
    9.   
    10.        //数值反传  
    11. private byte[] dataValueRollback(byte[] data) {  
    12.     ArrayList<Byte> al = new ArrayList<Byte>();  
    13.     for (int i = data.length - 1; i >= 0; i--) {  
    14.         al.add(data[i]);  
    15.     }  
    16.   
    17.     byte[] buffer = new byte[al.size()];  
    18.     for (int i = 0; i <= buffer.length - 1; i++) {  
    19.         buffer[i] = al.get(i);  
    20.     }  
    21.     return buffer;  
    22. }  




    方法二 
    先用 Float.floatToIntBits(f)转换成int 
    再通过如下方法转成byte [] 

    Java代码  收藏代码
    1. /** 
    2.  * 将int类型的数据转换为byte数组 原理:将int数据中的四个byte取出,分别存储 
    3.  *  
    4.  * @param n  int数据 
    5.  * @return 生成的byte数组 
    6.  */  
    7. public static byte[] intToBytes2(int n) {  
    8.     byte[] b = new byte[4];  
    9.     for (int i = 0; i < 4; i++) {  
    10.         b[i] = (byte) (n >> (24 - i * 8));  
    11.     }  
    12.     return b;  
    13. }  
    14.   
    15. /** 
    16.  * 将byte数组转换为int数据 
    17.  *  
    18.  * @param b 字节数组 
    19.  * @return 生成的int数据 
    20.  */  
    21. public static int byteToInt2(byte[] b) {  
    22.     return (((int) b[0]) << 24) + (((int) b[1]) << 16)  
    23.             + (((int) b[2]) << 8) + b[3];  
    24. }  



    方法三(这个是我在用的): 

    Java代码  收藏代码
    1. /** 
    2.  * 浮点转换为字节 
    3.  *  
    4.  * @param f 
    5.  * @return 
    6.  */  
    7. public static byte[] float2byte(float f) {  
    8.       
    9.     // 把float转换为byte[]  
    10.     int fbit = Float.floatToIntBits(f);  
    11.       
    12.     byte[] b = new byte[4];    
    13.     for (int i = 0; i < 4; i++) {    
    14.         b[i] = (byte) (fbit >> (24 - i * 8));    
    15.     }   
    16.       
    17.     // 翻转数组  
    18.     int len = b.length;  
    19.     // 建立一个与源数组元素类型相同的数组  
    20.     byte[] dest = new byte[len];  
    21.     // 为了防止修改源数组,将源数组拷贝一份副本  
    22.     System.arraycopy(b, 0, dest, 0, len);  
    23.     byte temp;  
    24.     // 将顺位第i个与倒数第i个交换  
    25.     for (int i = 0; i < len / 2; ++i) {  
    26.         temp = dest[i];  
    27.         dest[i] = dest[len - i - 1];  
    28.         dest[len - i - 1] = temp;  
    29.     }  
    30.       
    31.     return dest;  
    32.       
    33. }  
    34.   
    35. /** 
    36.  * 字节转换为浮点 
    37.  *  
    38.  * @param b 字节(至少4个字节) 
    39.  * @param index 开始位置 
    40.  * @return 
    41.  */  
    42. public static float byte2float(byte[] b, int index) {    
    43.     int l;                                             
    44.     l = b[index + 0];                                  
    45.     l &= 0xff;                                         
    46.     l |= ((long) b[index + 1] << 8);                   
    47.     l &= 0xffff;                                       
    48.     l |= ((long) b[index + 2] << 16);                  
    49.     l &= 0xffffff;                                     
    50.     l |= ((long) b[index + 3] << 24);                  
    51.     return Float.intBitsToFloat(l);                    
    52. }  




    2013-05-06 add 
    title Java基本类型与byte数组之间相互转换 
    from http://blog.sina.com.cn/s/blog_7a35101201012n0b.html 

    Java代码  收藏代码
      1. package com.my.wxf4j.utils;  
      2.   
      3. import java.nio.charset.Charset;  
      4.   
      5. public class ByteUtil  
      6. {  
      7.     public static byte[] getBytes(short data)  
      8.     {  
      9.         byte[] bytes = new byte[2];  
      10.         bytes[0] = (byte) (data & 0xff);  
      11.         bytes[1] = (byte) ((data & 0xff00) >> 8);  
      12.         return bytes;  
      13.     }  
      14.   
      15.     public static byte[] getBytes(char data)  
      16.     {  
      17.         byte[] bytes = new byte[2];  
      18.         bytes[0] = (byte) (data);  
      19.         bytes[1] = (byte) (data >> 8);  
      20.         return bytes;  
      21.     }  
      22.   
      23.     public static byte[] getBytes(int data)  
      24.     {  
      25.         byte[] bytes = new byte[4];  
      26.         bytes[0] = (byte) (data & 0xff);  
      27.         bytes[1] = (byte) ((data & 0xff00) >> 8);  
      28.         bytes[2] = (byte) ((data & 0xff0000) >> 16);  
      29.         bytes[3] = (byte) ((data & 0xff000000) >> 24);  
      30.         return bytes;  
      31.     }  
      32.   
      33.     public static byte[] getBytes(long data)  
      34.     {  
      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.     {  
      49.         int intBits = Float.floatToIntBits(data);  
      50.         return getBytes(intBits);  
      51.     }  
      52.   
      53.     public static byte[] getBytes(double data)  
      54.     {  
      55.         long intBits = Double.doubleToLongBits(data);  
      56.         return getBytes(intBits);  
      57.     }  
      58.   
      59.     public static byte[] getBytes(String data, String charsetName)  
      60.     {  
      61.         Charset charset = Charset.forName(charsetName);  
      62.         return data.getBytes(charset);  
      63.     }  
      64.   
      65.     public static byte[] getBytes(String data)  
      66.     {  
      67.         return getBytes(data, "GBK");  
      68.     }  
      69.   
      70.       
      71.     public static short getShort(byte[] bytes)  
      72.     {  
      73.         return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));  
      74.     }  
      75.   
      76.     public static char getChar(byte[] bytes)  
      77.     {  
      78.         return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));  
      79.     }  
      80.   
      81.     public static int getInt(byte[] bytes)  
      82.     {  
      83.         return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));  
      84.     }  
      85.      
      86.     public static long getLong(byte[] bytes)  
      87.     {  
      88.         return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] << 24))  
      89.          | (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));  
      90.     }  
      91.   
      92.     public static float getFloat(byte[] bytes)  
      93.     {  
      94.         return Float.intBitsToFloat(getInt(bytes));  
      95.     }  
      96.   
      97.     public static double getDouble(byte[] bytes)  
      98.     {  
      99.         long l = getLong(bytes);  
      100.         System.out.println(l);  
      101.         return Double.longBitsToDouble(l);  
      102.     }  
      103.   
      104.     public static String getString(byte[] bytes, String charsetName)  
      105.     {  
      106.         return new String(bytes, Charset.forName(charsetName));  
      107.     }  
      108.   
      109.     public static String getString(byte[] bytes)  
      110.     {  
      111.         return getString(bytes, "GBK");  
      112.     }  
      113.   
      114.       
      115.     public static void main(String[] args)  
      116.     {  
      117.         short s = 122;  
      118.         int i = 122;  
      119.         long l = 1222222;  
      120.   
      121.         char c = 'a';  
      122.   
      123.         float f = 122.22f;  
      124.         double d = 122.22;  
      125.   
      126.         String string = "我是好孩子";  
      127.         System.out.println(s);  
      128.         System.out.println(i);  
      129.         System.out.println(l);  
      130.         System.out.println(c);  
      131.         System.out.println(f);  
      132.         System.out.println(d);  
      133.         System.out.println(string);  
      134.   
      135.         System.out.println("**************");  
      136.   
      137.         System.out.println(getShort(getBytes(s)));  
      138.         System.out.println(getInt(getBytes(i)));  
      139.         System.out.println(getLong(getBytes(l)));  
      140.         System.out.println(getChar(getBytes(c)));  
      141.         System.out.println(getFloat(getBytes(f)));  
      142.         System.out.println(getDouble(getBytes(d)));  
      143.         System.out.println(getString(getBytes(string)));  
      144.     }  
      145. }  
  • 相关阅读:
    mysql MHA报错 Can't exec "mysqlbinlog": No such file or directory at /usr/local/share/perl5/MHA/BinlogManager.pm line 99.
    树莓派搭建私人服务器
    动手写简单的嵌入式操作系统一
    java 返回json数据
    C语言中内存分配
    IntelliJ IDEA14.0.3+Maven+SpringMVC+Spring+Hibernate光速构建Java权限管理系统(三)
    linux设备驱动归纳总结
    阿里云centos6.5下搭建javaWeb运行环境
    JAVAWEB项目如何实现验证码
    Linux驱动开发:USB驱动之usb_skel分析
  • 原文地址:https://www.cnblogs.com/wi100sh/p/5171013.html
Copyright © 2011-2022 走看看