登录  /  注册
首页 > Java > java教程 > 正文

Java字节流与基本数据类型的转换实例详解

怪我咯
发布: 2017-07-02 10:18:38
原创
2031人浏览过

本篇文章主要介绍了Java字节流与基本数据类型的转换实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在实际开发中,我们经常遇到与嵌入式进行通信的情况,而由于一些嵌入式设备的处理能力较差,往往以二进制的数据流的形式传输数据,在此将这些常见的转换做一总结。

注意:默认传输时使用小端模式

将字节流转换为int类型数据

public static int getInt(byte[] bytes) {
  return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16))
      | (0xff000000 & (bytes[3] << 24));
}
登录后复制

将字节流转换为long类型数据

public static long getLong(byte[] bytes) {
  return ((0xffL & (long) bytes[0]) | (0xff00L & ((long) bytes[1] << 8)) | (0xff0000L & ((long) bytes[2] << 16))
      | (0xff000000L & ((long) bytes[3] << 24)) | (0xff00000000L & ((long) bytes[4] << 32))
      | (0xff0000000000L & ((long) bytes[5] << 40)) | (0xff000000000000L & ((long) bytes[6] << 48))
      | (0xff00000000000000L & ((long) bytes[7] << 56)));
}
登录后复制

将字节流转换为float类型数据

public static float getFloat(byte[] bytes){
  int temp=getInt(bytes);
  return Float.intBitsToFloat(temp);
}
登录后复制

将字节流转换为double类型数据

public static double getDouble(byte[] bytes){
  long temp=getLong(bytes);
  return Double.longBitsToDouble(temp);
}
登录后复制

将int类型数据转换为字节流

public static byte[] getByteFromInt(int data){
  byte[] temp=new byte[4];
  temp[0]=(byte)(0xFF&(data));
  temp[1]=(byte)(0xFF&(data>>8));
  temp[2]=(byte)(0xFF&(data>>16));
  temp[3]=(byte)(0xFF&(data>>24));
  return temp;
}
登录后复制

将long类型数据转换为字节流

public static byte[] getByteFromLong(long data){
  byte[] temp=new byte[8];
  temp[0]=(byte)(0xFF&(data));
  temp[1]=(byte)(0xFF&(data>>8));
  temp[2]=(byte)(0xFF&(data>>16));
  temp[3]=(byte)(0xFF&(data>>24));
  temp[4]=(byte)(0xFF&(data>>32));
  temp[5]=(byte)(0xFF&(data>>40));
  temp[6]=(byte)(0xFF&(data>>48));
  temp[7]=(byte)(0xFF&(data>>56));
  return temp;
}
登录后复制

将float类型数据转换为字节流

public static byte[] getByteFromFloat(float data){
  byte[] temp=new byte[4];
  int tempInt=Float.floatToIntBits(data);
  temp[0]=(byte)(0xFF&(tempInt));
  temp[1]=(byte)(0xFF&(tempInt>>8));
  temp[2]=(byte)(0xFF&(tempInt>>16));
  temp[3]=(byte)(0xFF&(tempInt>>24));
  return temp;
}
登录后复制

将double类型数据转换为字节流

public static byte[] getByteFromDouble(double data){
  byte[] temp=new byte[8];
  long tempLong=Double.doubleToLongBits(data);
  temp[0]=(byte)(0xFF&(tempLong));
  temp[1]=(byte)(0xFF&(tempLong>>8));
  temp[2]=(byte)(0xFF&(tempLong>>16));
  temp[3]=(byte)(0xFF&(tempLong>>24));
  temp[4]=(byte)(0xFF&(tempLong>>32));
  temp[5]=(byte)(0xFF&(tempLong>>40));
  temp[6]=(byte)(0xFF&(tempLong>>48));
  temp[7]=(byte)(0xFF&(tempLong>>56));
  return temp;
}
登录后复制

以上就是Java字节流与基本数据类型的转换实例详解的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学