package com.jove.demo.utils;
import java.nio.charset.Charset;
import java.util.List;
public class ByteHelper {
/**
* 字节数组加1
*/
public static synchronized byte[] BytesPP(byte[] source) {
byte[] result = new byte[2];
if (source[0] == (byte) 0xFF && source[1] == (byte) 0xFF) {
result = new byte[]{(byte) 0x00, (byte) 0x00};
} else if (source[1] == (byte) 0xFF) {
result[0] = (byte) (source[0] + 1);
result[1] = (byte) 0x00;
} else {
result[0] = source[0];
result[1] = (byte) (source[1] + 1);
}
return result;
}
/**
* 合并两个字节数组
*/
public static byte[] Composite2Bytes(byte[] bytesA, byte[] bytesB) {
byte[] result = new byte[bytesA.length + bytesB.length];
System.arraycopy(bytesA, 0, result, 0, bytesA.length);
System.arraycopy(bytesB, 0, result, bytesA.length, bytesB.length);
return result;
}
/**
* 合并多个字节数组
*/
public static byte[] CompositeBytes(List<byte[]> list) {
byte[] result = null;
for (int i = 0; i < list.size(); i++) {
byte[] temp;
if (null == result) {
temp = new byte[]{};
} else {
temp = result;
}
result = Composite2Bytes(temp, list.get(i));
}
return result;
}
/**
* byte[]倒序
*/
public static byte[] ReverseBytes(byte[] b) {
byte[] result = new byte[b.length];
for (int i = 0; i < b.length; i++) {
result[b.length - 1 - i] = b[i];
}
return result;
}
// /**
// * 将byte[]转换为2进制数组成的字符串
// *
// * @param b
// * @return
// */
// public static String BytesToBinaryString(byte[] b) {
// String temp = new BigInteger(1, b).toString(2);
// int total = 8 * b.length;
// int dif = total - temp.length();
// StringBuffer sb = new StringBuffer();
// for (int i = 0; i < dif; i++) {
// sb.append("0");
// }
// return sb.append(temp).toString();
// }
/**
* 将byte[]转换为2进制数组成的字符串(每个字节倒序)
*/
public static String BytesToBinaryString(byte[] b) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < b.length; i++) {
sb.append(ByteToBinaryString(b[i]));
}
return sb.toString();
}
/**
* Byte转2进制数组成的字符串(倒序)
*/
public static String ByteToBinaryString(byte b) {
return "" + (byte) ((b >> 0) & 0x1) + (byte) ((b >> 1) & 0x1)
+ (byte) ((b >> 2) & 0x1) + (byte) ((b >> 3) & 0x1)
+ (byte) ((b >> 4) & 0x1) + (byte) ((b >> 5) & 0x1)
+ (byte) ((b >> 6) & 0x1) + (byte) ((b >> 7) & 0x1);
}
/**
* 将byte[]转换为16进制数组成的字符串
*/
public static String BytesToHexString(byte[] b, int length) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
result.append(hex.toUpperCase());
}
return result.toString();
}
/**
* 将byte[]转换为16进制数组成的字符串
*/
public static String BytesToHexString(byte[] b) {
return BytesToHexString(b, b.length);
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
/**
* 将16进制数组成的字符串转换为byte[]
*/
public static byte[] HexStringToBytes(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i < len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
}
return result;
}
/**
* char转byte[]
*/
public static byte[] CharToBytes(char c) {
byte[] b = new byte[2];
b[0] = (byte) ((c & 0xFF00) >> 8);
b[1] = (byte) (c & 0xFF);
return b;
}
/**
* byte[]转char
*/
public static char BytesToChar(byte[] b) {
char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
return c;
}
/**
* byte[]转String(gb2312编码)
*/
public static String BytesToString(byte[] b) {
return new String(b, Charset.forName("gb2312")).trim();
}
/**
* String转byte[](gb2312编码)
*/
public static byte[] StringToBytes(String s) {
return s.getBytes(Charset.forName("gb2312"));
}
/**
* String转byte[],并补0(gb2312编码)
*/
public static byte[] StringToBytes(String s, int length) {
byte[] b = StringToBytes(s);
if (b.length > length) {
return null;
}
byte[] temp = new byte[length - b.length];
return Composite2Bytes(b, temp);
}
/**
* 将int数值转换为byte
*/
public static byte IntToByte(int value) {
return (byte) ((value & 0xFF));
}
/**
* 将int数值转换为占1个字节的byte数组
*
* @param value 要转换的int值
* @return byte数组
*/
public static byte[] IntToBytes(int value) {
byte[] byte_src = new byte[1];
byte_src[0] = (byte) ((value & 0xFF));
return byte_src;
}
/**
* int转2字节数组(高位在前,低位在后)
*/
public static byte[] IntToBytes2(int value) {
byte[] src = new byte[2];
src[0] = (byte) ((value >> 8) & 0xFF);
src[1] = (byte) (value & 0xFF);
return src;
}
/**
* int转4字节数组(高位在前,低位在后)
*/
public static byte[] IntToBytes4(int value) {
byte[] src = new byte[4];
src[0] = (byte) ((value >> 24) & 0xFF);
src[1] = (byte) ((value >> 16) & 0xFF);
src[2] = (byte) ((value >> 8) & 0xFF);
src[3] = (byte) (value & 0xFF);
return src;
}
/**
* 4字节数组转int(高位在前,低位在后)
*/
public static int Bytes4ToInt(byte[] src) {
int value;
value = (((src[0] & 0xFF) << 24) | ((src[1] & 0xFF) << 16)
| ((src[2] & 0xFF) << 8) | (src[3] & 0xFF));
return value;
}
/**
* 浮点转换为字节
*/
public static byte[] FloatToBytes4(float f) {
// 把float转换为byte[]
int fbit = Float.floatToIntBits(f);
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (fbit >> (24 - i * 8));
}
return b;
}
/**
* 字节转换为浮点
*
* @param b 字节
*/
public static float Bytes4ToFloat(byte[] b) {
// int l;
// l = b[0];
// l &= 0xff;
// l |= ((long) b[1] << 8);
// l &= 0xffff;
// l |= ((long) b[2] << 16);
// l &= 0xffffff;
// l |= ((long) b[3] << 24);
int accum = 0;
accum = accum | (b[0] & 0xff) << 24;
accum = accum | (b[1] & 0xff) << 16;
accum = accum | (b[2] & 0xff) << 8;
accum = accum | (b[3] & 0xff);
return Float.intBitsToFloat(accum);
}
/**
* 2字节数组转int(高位在前,低位在后)
*/
public static int Bytes2ToInt(byte[] src) {
int value;
value = (((src[0] & 0xFF) << 8) | ((src[1] & 0xFF)));
return value;
}
/**
* byte[]中7D转7D5D
*/
public static byte[] Bytes7D(byte[] bytes) {
byte[] result;
int offset = 0;//偏移量
for (int i = 0; i < bytes.length; i++) {
if ((byte) 0x7D == bytes[i]) {
offset++;
}
}
if (0 == offset) {
return bytes;
}
result = new byte[bytes.length + offset];
offset = 0;
for (int i = 0; i < bytes.length; i++) {
if ((byte) 0x7D == bytes[i]) {
result[i + offset] = (byte) 0x7D;
result[i + offset + 1] = (byte) 0x5D;
offset++;
} else {
result[i + offset] = bytes[i];
}
}
return result;
}
/**
* byte[]中7E转7D5E
*/
public s
没有合适的资源?快使用搜索试试~ 我知道了~
USB-OTG连接Demo
共35个文件
png:10个
xml:7个
java:5个
4星 · 超过85%的资源 需积分: 49 27 下载量 18 浏览量
2017-05-17
08:38:07
上传
评论 2
收藏 157KB ZIP 举报
温馨提示
简单的USB-OTG 连接以及数据接收Demo,食用方法:http://blog.csdn.net/nhh0905/article/details/72304460
资源推荐
资源详情
资源评论
收起资源包目录
Demo.zip (35个子文件)
Demo
gradlew 5KB
settings.gradle 35B
.DS_Store 6KB
gradle
wrapper
gradle-wrapper.properties 230B
gradle-wrapper.jar 52KB
local.properties 525B
gradlew.bat 2KB
gradle.properties 730B
app
.DS_Store 6KB
src
androidTest
java
com
jove
demo
ExampleInstrumentedTest.java 713B
test
java
com
jove
demo
ExampleUnitTest.java 382B
main
AndroidManifest.xml 741B
res
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 4KB
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 14KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 6KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 2KB
drawable
layout
device_item.xml 482B
activity_device_list.xml 1KB
activity_device_info.xml 762B
values
colors.xml 241B
strings.xml 168B
styles.xml 2KB
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 10KB
java
com
jove
demo
utils
ByteHelper.java 10KB
activity
DeviceListActivity.java 4KB
DeviceInfoActivity.java 1KB
libs
UsbSerialDriver-release.aar 22KB
build.gradle 2KB
proguard-rules.pro 939B
build.gradle 498B
共 35 条
- 1
资源评论
- xdfeng20082017-11-03很好的资源
- 耗子jerry2017-07-25可一参考学习
- 数字化转型高高老师2022-12-28gradle 7.4版本无法运行,compile无法使用,同时java文件引入的库过期,无法调试 recyclerview 总是报错。 总之 我下载代码无法运行,日期2022年12月28日 #参考意义不大
nhh0905
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Python大作业:音乐播放软件(爬虫+可视化+数据分析+数据库)
- 课程设计-python爬虫-爬取日报,爬取日报文章后存储到本地,附带源代码+课程设计报告
- 软件和信息技术服务行业投资与前景预测.pptx
- 课程设计-基于SpringBoot + Mybatis+python爬虫NBA球员数据爬取可视化+源代码+文档+sql+效果图
- 软件品质管理系列二项目策划规范.doc
- 基于TensorFlow+PyQt+GUI的酒店评论情感分析,支持分析本地数据文件和网络爬取数据分析+源代码+文档说明+安装教程
- 软件定义无线电中的模拟电路测试技术.pptx
- 软件开发协议(作为技术开发合同附件).doc
- 软件开发和咨询行业技术趋势分析.pptx
- 软件测试题详解及答案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功