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
*
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SerialPortDemo.zip (41个子文件)
SerialPortDemo
bin
com
yang
serialport
exception
TooManyListeners.class 554B
SerialPortParameterFailure.class 590B
SerialPortInputStreamCloseFailure.class 610B
SerialPortOutputStreamCloseFailure.class 617B
ExceptionWriter.class 2KB
SendDataToSerialPortFailure.class 566B
PortInUse.class 530B
NotASerialPort.class 563B
NoSuchPort.class 563B
ReadDataFromSerialPortFailure.class 575B
manage
SerialPortManager.class 5KB
ui
MainFrame.class 8KB
MainFrame$SerialListener.class 2KB
MainFrame$3.class 579B
MainFrame$2.class 826B
MainFrame$1.class 1KB
utils
ByteUtils.class 7KB
ShowUtils.class 1KB
SerialPortDemo.jar 141KB
.settings
org.eclipse.core.resources.prefs 88B
org.eclipse.jdt.core.prefs 629B
src
com
yang
serialport
exception
NotASerialPort.java 308B
SerialPortOutputStreamCloseFailure.java 342B
SerialPortInputStreamCloseFailure.java 336B
PortInUse.java 280B
ExceptionWriter.java 1KB
ReadDataFromSerialPortFailure.java 305B
SerialPortParameterFailure.java 323B
NoSuchPort.java 312B
TooManyListeners.java 297B
SendDataToSerialPortFailure.java 298B
manage
SerialPortManager.java 6KB
ui
MainFrame.java 8KB
utils
ByteUtils.java 12KB
ShowUtils.java 1KB
.project 390B
.fatjar 357B
.classpath 413B
lib
AbsoluteLayout.jar 3KB
RXTXcomm.jar 60KB
swing-layout-1.0.3.jar 115KB
共 41 条
- 1
容华谢后
- 粉丝: 465
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页