package com.yang.serialport.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Locale;
public class ByteUtils {
/**
* 用来把mac字符串转换为long
*
* @param strMac
* @return
*/
public static long macToLong(String strMac) {
byte[] mb = new BigInteger(strMac, 16).toByteArray();
ByteBuffer mD = ByteBuffer.allocate(mb.length);
mD.put(mb);
long mac = 0;
// 如果长度等于8代表没有补0;
if (mD.array().length == 8) {
mac = mD.getLong(0);
} else if (mD.array().length == 9) {
mac = mD.getLong(1);
}
return mac;
}
public static byte[] getBytes(Object obj) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bout);
out.writeObject(obj);
out.flush();
byte[] bytes = bout.toByteArray();
bout.close();
out.close();
return bytes;
}
/**
* 函数名称:hexStr2Byte</br> 功能描述:String 转数组
*
* @param hex
* @return 修改日志:</br>
* <table>
* <tr>
* <th>版本</th>
* <th>日期</th>
* <th>作者</th>
* <th>描述</th>
* <tr>
* <td>0.1</td>
* <td>2014-7-16</td>
* <td>ZhaoQing</td>
* <td>初始创建</td>
* </table>
* @author ZhaoQing
*/
public static byte[] hexStr2Byte(String hex) {
ByteBuffer bf = ByteBuffer.allocate(hex.length() / 2);
for (int i = 0; i < hex.length(); i++) {
String hexStr = hex.charAt(i) + "";
i++;
hexStr += hex.charAt(i);
byte b = (byte) Integer.parseInt(hexStr, 16);
bf.put(b);
}
return bf.array();
}
/**
* 函数名称:byteToHex</br> 功能描述:byte转16进制
*
* @param b
* @return 修改日志:</br>
* <table>
* <tr>
* <th>版本</th>
* <th>日期</th>
* <th>作者</th>
* <th>描述</th>
* <tr>
* <td>0.1</td>
* <td>2014-6-26</td>
* <td>ZhaoQing</td>
* <td>初始创建</td>
* </table>
* @author ZhaoQing
*/
public static String byteToHex(byte b) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
return hex.toUpperCase(Locale.getDefault());
}
public static Object getObject(byte[] bytes) throws IOException,
ClassNotFoundException {
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bi);
Object obj = oi.readObject();
bi.close();
oi.close();
return obj;
}
public static ByteBuffer getByteBuffer(Object obj) throws IOException {
byte[] bytes = ByteUtils.getBytes(obj);
ByteBuffer buff = ByteBuffer.wrap(bytes);
return buff;
}
/**
* byte[] 转short 2字节
*
* @param bytes
* @return
*/
public static short bytesToshort(byte[] bytes) {
return (short) ((bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00));
}
/**
* byte 转Int
*
* @param b
* @return
*/
public static int byteToInt(byte b) {
return (b) & 0xff;
}
public static int bytesToInt(byte[] bytes) {
int addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
addr |= ((bytes[2] << 16) & 0xFF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
return addr;
}
public static byte[] intToByte(int i) {
byte[] abyte0 = new byte[4];
abyte0[0] = (byte) (0xff & i);
abyte0[1] = (byte) ((0xff00 & i) >> 8);
abyte0[2] = (byte) ((0xff0000 & i) >> 16);
abyte0[3] = (byte) ((0xff000000 & i) >> 24);
return abyte0;
}
public static byte[] LongToByte(Long i) {
byte[] abyte0 = new byte[8];
abyte0[0] = (byte) (0xff & i);
abyte0[1] = (byte) ((0xff00 & i) >> 8);
abyte0[2] = (byte) ((0xff0000 & i) >> 16);
abyte0[3] = (byte) ((0xff000000 & i) >> 24);
abyte0[4] = (byte) ((0xff00000000l & i) >> 32);
abyte0[5] = (byte) ((0xff0000000000l & i) >> 40);
abyte0[6] = (byte) ((0xff000000000000l & i) >> 48);
abyte0[7] = (byte) ((0xff00000000000000l & i) >> 56);
return abyte0;
}
/**
* 函数名称:shortChange</br> 功能描述:short 大端转小端
*
* @param mshort
* @return 修改日志:</br>
* <table>
* <tr>
* <th>版本</th>
* <th>日期</th>
* <th>作者</th>
* <th>描述</th>
* <tr>
* <td>0.1</td>
* <td>2014-6-26</td>
* <td>ZhaoQing</td>
* <td>初始创建</td>
* </table>
* @author ZhaoQing
*/
public static short shortChange(Short mshort) {
mshort = (short) ((mshort >> 8 & 0xFF) | (mshort << 8 & 0xFF00));
return mshort;
}
/**
* 函数名称:intChange</br> 功能描述:int 大端转小端
*
* @param mint
* @return 修改日志:</br>
* <table>
* <tr>
* <th>版本</th>
* <th>日期</th>
* <th>作者</th>
* <th>描述</th>
* <tr>
* <td>0.1</td>
* <td>2014-6-26</td>
* <td>ZhaoQing</td>
* <td>初始创建</td>
* </table>
* @author ZhaoQing
*/
public static int intChange(int mint) {
mint = (int) (((mint) >> 24 & 0xFF) | ((mint) >> 8 & 0xFF00)
| ((mint) << 8 & 0xFF0000) | ((mint) << 24 & 0xFF000000));
return mint;
}
/**
* 函数名称:intChange</br> 功能描述:LONG 大端转小端
*
* @param mint
* @return 修改日志:</br>
* <table>
* <tr>
* <th>版本</th>
* <th>日期</th>
* <th>作者</th>
* <th>描述</th>
* <tr>
* <td>0.1</td>
* <td>2014-6-26</td>
* <td>ZhaoQing</td>
* <td>初始创建</td>
* </table>
* @author ZhaoQing
*/
public static long longChange(long mint) {
mint = (long) (((mint) >> 56 & 0xFF) | ((mint) >> 48 & 0xFF00)
| ((mint) >> 24 & 0xFF0000) | ((mint) >> 8 & 0xFF000000)
| ((mint) << 8 & 0xFF00000000l)
| ((mint) << 24 & 0xFF0000000000l)
| ((mint) << 40 & 0xFF000000000000l) | ((mint) << 56 & 0xFF00000000000000l));
return mint;
}
/**
* 将byte转换为无符号的short类型
*
* @param b
* 需要转换的字节数
* @return 转换完成的short
*/
public static short byteToUshort(byte b) {
return (short) (b & 0x00ff);
}
/**
* 将byte转换为无符号的int类型
*
* @param b
* 需要转换的字节数
* @return 转换完成的int
*/
public static int byteToUint(byte b) {
return b & 0x00ff;
}
/**
* 将byte转换为无符号的long类型
*
* @param b
* 需要转换的字节数
* @return 转换完成的long
*/
public static long byteToUlong(byte b) {
return b & 0x00ff;
}
/**
* 将short转换为无符号的int类型
*
* @param s
* 需要转换的short
* @return 转换完成的int
*/
public static int shortToUint(short s) {
return s & 0x00ffff;
}
/**
* 将short转换为无符号的long类型
*
* @param s
* 需要转换的字节数
* @return 转换完成的long
*/
public static long shortToUlong(short s) {
return s & 0x00ffff;
}
/**
* 将int转换为无符号的long类型
*
* @param i
* 需要转换的字节数
* @return 转换完成的long
*/
public static long intToUlong(int i) {
return i & 0x00ffffffff;
}
/**
* 将short转换成小端序的byte数组
*
* @param s
*
java串口调试demo
5星 · 超过95%的资源 需积分: 50 173 浏览量
2016-08-24
11:37:21
上传
评论 9
收藏 269KB ZIP 举报
容华谢后
- 粉丝: 465
- 资源: 38
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页