Java 中数据类型转换成 byte[] 方法

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

[java]  view plain copy
  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]  view plain copy
  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]  view plain copy
  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]  view plain copy
  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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值