java常用二进制数据转换工具
经常需要与plc,单片机,传感器通讯,需要解析设备数据,java跟C不同,需要二进制转换成java的数据,因此整理了一个工具,共享出来,欢迎大家来指正。
/**
* 常用二进制数据转换工具
* @author qujia
*
*/
public class ByteTool {
/**
* int 转换为二进制,4个字节,plc里面word
* @return
*/
public static byte[] IntToByte(int v)
{
//System.out.print("int value="+v);
byte[] arr=new byte[4];
arr[0]=(byte)(v&0x000000ff);
v>>=8;//右移8位
arr[1]=(byte)(v&0x000000ff);
v>>=8;//右移8位
arr[2]=(byte)(v&0x000000ff);
v>>=8;//右移8位
arr[3]=(byte)(v&0x000000ff);
return arr;
}
/**
* int 转换位short,plc里面的int
* @return
*/
public static byte[] ShortToByte(short v)
{
byte[] arr=new byte[2];
arr[0]=(byte)(v&0x00ff);
v>>=8;//锟斤拷锟斤拷8位
arr[1]=(byte)(v&0x00ff);
return arr;
}
/**
* Hex数组转换为字符串
* @param cmd
* @param len
* @return
*/
public static String arrToString(byte [] cmd,int len) {
StringBuffer sb=new StringBuffer();
for(int i=0;i<len;i++)sb.append(String.format("%02X ", cmd[i]));
//System.out.println(sb.toString());
return sb.toString();
}
/**
* 二进制的字符串转换位byte数组
* @param s
* @return
*/
public static byte[] stringToArray(String s) {
byte[] arr=new byte[s.length()/2];
int t=0;
for(int i=0;i<s.length()-1;i+=2) {
char a=s.charAt(i);
char b=s.charAt(i+1);
arr[t]=(byte)(hexToInt(a)*16+hexToInt(b));
t++;
}
return arr;
}
/**
* 10进制字符转换位整数
* @param a
* @return
*/
private static int hexToInt(char a) {
if(a>='0' && a<='9')return (int)(a-'0');
if(a>='A' && a<='Z') return 10+(a-'A');
//System.out.println("charvalue = "+a);
return 0;
}
/***
* 二进制转换为字符串0010.....
* @param b
*/
public static String byteToString(byte b) {
StringBuffer sb=new StringBuffer();
byte a=0x01;
for(int i=0;i<8;i++) {
if(0== (a&b)) {
sb.insert(0, "0");
}
else {
sb.insert(0, "1");
}
a<<=1;//左移1位
}
return sb.toString();
}
/**
* 字节转换int,高到低
* @param bytes 二进制数组
* @return
*/
public static int byteArrayToInt2(byte[] data,int start) {
int value=0;
for(int i = start; i < start+4; i++) {
int shift= i * 8;
value +=(data[i] & 0xFF) << shift;
}
return value;
}
/**
* 字节转换int,低到高
* @param bytes 二进制数组
* @return
*/
public static int byteArrayToInt(byte[] data,int start) {
int value=0;
for(int i = start; i < start+4; i++) {
int shift= (start+3-i) * 8;
value +=(data[i] & 0xFF) << shift;
}
return value;
}
/**
* 字节转short
* @param bytes
* @param start
* @return
*/
public static short byteArrayToShort(byte[] bytes,int start)
{
short v=0;
v=(short)((bytes[start]&0xff)<<8 | bytes[start+1]&0xff);//温度11-12位
return v;
}
/***
* byte数组转换为float,低位到高位
* @param arr
* @param index 数据开始下标
* @return
*/
public static float getFloat(byte[] arr, int index) {
ByteBuffer buf=ByteBuffer.allocateDirect(4);
buf.put(arr,index,4);
buf=buf.order(ByteOrder.LITTLE_ENDIAN);//方向转换
buf.rewind();
float f=buf.getFloat();
return f;
}
/***
* byte数组转换为float,高位位到低位
* @param arr
* @param index 数据开始下标
* @return
*/
public static float getFloat2(byte[] arr, int index) {
ByteBuffer buf=ByteBuffer.allocateDirect(4);
buf.put(arr,index,4);
buf=buf.order(ByteOrder.BIG_ENDIAN);//方向转换
buf.rewind();
float f=buf.getFloat();
return f;
}
}
java常用二进制数据转换工具相关推荐
- java常用的数据转换工具
public class ByteUtil {/*** 将多个byte数据,整合到一个数组中*/public static byte[] combineByteArrays(byte[] ... by ...
- java常用的时间工具—原日期的基础上增加时间
java常用的时间工具 1.在原日期的基础上增加天数 /*** 在原日期的基础上增加天数*/ public static Date add(Date date, int num) {Calendar ...
- JAVA 常用框架和工具
集成开发工具(IDE):Eclipse.MyEclipse.Spring Tool Suite(STS).Intellij IDEA.NetBeans.JBuilder.JCreator JAVA服务 ...
- Java 常用HTTP请求工具类HttpUtils
.pom依赖 <!-- httpclient --><dependency><groupId>org.apache.httpcomponents</group ...
- 【云驻共创】 JAVA常用的开发工具有哪些?
前言 俗话说,工欲善其事,必先利其器.工匠想要使他的工作做好,一定要先让工具锋利.比喻要做好一件事,准备工具非常重要.对于我们做程序员的来说也是一样的,想要提高开发效率,也需要选择好自己的开发工具,下 ...
- java常用压测工具_几款常用压测工具推荐
ab ab是apache自带的压力测试工具,使用起来非常方便. 安装 如果安装了apache, 那么ab已经安装好了,如果不想安装apache的话,可以通过以下方式安装ab # ubuntu sudo ...
- Java常用多线程辅助工具---countdownLatch
为什么80%的码农都做不了架构师?>>> 前言 上一篇博文说到semaphore,一个加强版的synchronized,该多线程辅助工具适用于控制对资源操作或者访问的场景.现在有一张 ...
- Java常用性能分析工具 jconsole、jvisualvm、 jstat、jinfo、jmap、jhat、jstack
1.jconsole 2.jvisualvm 3.jstat 4.jinfo 5.jmap 6.jhat 7.jstack
- java 文件拷贝保留原来的属性_Java常用属性拷贝工具类使用总结
开头聊几句 1.网上很多的技术文章和资料是有问题的,要学会辨证的看待,不能随便就拿来用,起码要自己验证一下 2.关注当下,关注此刻,如果你真正阅读本篇文章,请花几分钟时间的注意力阅读,相信你会有收获的 ...
最新文章
- vue ts 设置tslint提示_Typescript 在 Vue 中的实践(包含2.x、3.x)
- python四十八:多态
- linux下使用rdesktop连接远程windows
- loadrunner性能测试---添加windows多台压力机
- 第一次立会(2019.3.24)
- fatal error C1010: unexpected end of file while looking for precompiled header directive
- 用Python搭建“冲顶大会”外挂,王思聪们还舍得撒币吗?
- Java Spring-事务管理
- dijkastra算法实践poj2387
- 循环结构:while和do...while循环语句
- C# 写入CSV文件
- UML-类 图 (2)
- 手游后劲不足,“体验”会是端游发展的一张王牌吗?
- Alarm Clock C/C++ Version
- mac 查看端口_交换机端口对应的mac地址与IP地址
- win7下如何注册控件
- Ajax跨域请求时出现Access to XMLHttpRequest at ‘xxx‘ from origin ‘xxx‘ has been been blocked by CORS policy
- apk v1+v2命令行签名命令
- 章鱼未来之星获得25万美金奖励|章鱼加速器2022夏季创业营圆满落幕
- 23种设计模式之Java实现