byte处理的几种方法
/**
* 字符串转16进制byte
* @param
* @return
* @throws Exception
* @author hw
* @date 2018/10/19 9:47
*/
private static byte hexStr2Str(String hexStr) {
String str = "0123456789abcdef";
char[] hexs = hexStr.toCharArray();
int n = 0;
n = str.indexOf(hexs[0]) * 16;
n += str.indexOf(hexs[1]);
byte bytes = (byte) (n & 0xff); return bytes;
}
输入:"80"
输出:0x80 , 打印出为 -128
注:
0x80 表示 128,(0x 代表 16 进制,8 * 16¹ + 0 * 16º = 128),128 的二进制是 10000000,即 2 的 7 次方。
byte 共有 8 位,表示范围是 -128 ~ 127,二进制即 10000000 ~ 01111111,第一位为符号位,1 表示负数,0 表示整数,11111111 即表示 -127,10000000 比较特殊,表示 -128。
所以,0x80 本来是整数的 128,二进制 00000000000000000000000010000000 (Java 中整数4个字节32位)。(byte)0x80,将其转换为 byte,即截取最后 8 位,即 10000000,就是 byte 中的 -128。
/**
* 将16进制字符串转换为byte[]
*
* @param str
* @return
*/
public static byte[] toBytes(String str) {
if(str == null || str.trim().equals("")) {
return new byte[0];
} byte[] bytes = new byte[str.length() / 2];
for(int i = 0; i < str.length() / 2; i++) {
String subStr = str.substring(i * 2, i * 2 + 2);
bytes[i] = (byte) Integer.parseInt(subStr, 16);
} return bytes;
}
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "80b58be8af95";
System.out.println("转换后的字节数组:" + Arrays.toString(toBytes(str)));
}
输出:
转换后的字节数组:[-128, -75, -117, -24, -81, -107]
/**
* 将数组以字符串形式存储
* @param
* @return
* @throws Exception
* @author hw
* @date 2018/10/19 9:45
*/
public static void saveFile2(byte[] bfile, String filename) throws IOException {
String fileURI ="D:\\" + filename;
try {
File file = new File(fileURI);
if(!file.exists()){
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fw);
out.write(toHexString1(bfile));
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} public static String toHexString1(byte[] b){
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; ++i){
buffer.append(toHexString1(b[i]));
}
String str = buffer.toString().substring(0,buffer.length()-1);
return str;
} public static String toHexString1(byte b){
String s = Integer.toHexString(b & 0xFF);
if (s.length() == 1){
return "0" + s +",";
}else{
return s+",";
}
}
输入:byte[] bytes1 = { (byte)0xE2, (byte)0xa8, 0x34, (byte)0x80,(byte)0x91,0x22,0x33,0x44,0x55,0x66,0x77};
输出:E2, a8,34,80,91,22,33,44,55,66,77
/**
* 读取文件
* @param
* @return
* @throws Exception
* @author hw
* @date 2018/10/17 19:59
*/
public static byte[] readFile(String filePath) throws IOException {
File file = new File(filePath);
ByteArrayOutputStream out = null;
FileInputStream in =null;
try {
in = new FileInputStream(file);
out = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) != -1) {
String str = new String(b,0,i);
out.write(str.getBytes(), 0, str.getBytes().length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
out.close();
in.close();
} byte[] bytes = out.toByteArray();
return bytes;
}
/**
* 读取文件存为byte[]
* @param
* @return
* @throws Exception
* @author hw
* @date 2018/10/19 9:48
*/
@ResponseBody
@RequestMapping(value = "/fileToByte", method = RequestMethod.GET)
public void fileToByte() throws Exception {
String filePath1 = "D:\\test1.txt";
byte[] bytes1 = readFile(filePath1);
String str1= new String (bytes1);
String[] strs1 = str1.split(","); String filePath2 = "D:\\test2.txt";
byte[] bytes2 = readFile(filePath2);
String str2 = new String(bytes2); String[] strs2 = str2.split(",");
byte[] bytes3 = new byte[strs2.length];
for(int i = 0 ; i<strs2.length ; i++){
bytes3[i] =hexStr2Str(strs2[i]);
} // String[] strs1={"2","5","7","9"};
// String[] strs2 = {"e2","af","34","80","11","22","33","44","55","66","77"}; int start = 0;
int end = 0;
for(int i = 0; i<strs1.length;i++){
end = Integer.valueOf(strs1[i]);
byte[] byte_1 = new byte[end - start]; System.arraycopy(bytes3, start, byte_1, 0, end - start);
start = end; bytes.offer(byte_1);
System.out.println("处理文件当前队列大小" + bytes.size());
}
}
byte处理的几种方法的更多相关文章
- C# byte[]数组和string的互相转化 (四种方法)
C# byte[]数组和string的互相转化 (四种方法) 第一种 [csharp] view plain copy string str = System.Text.Encoding.UTF8.G ...
- delphi中将 4 个 Byte 合成 1 个 Integer 的五种方法
有4个字节类型的值,用移位或逻辑运算符怎么合成一个整数?比如 $FFEEDDCC.高$FF$EE$DD$CC低 //方法 1: 共用内存procedure TForm1.Button1Click(Se ...
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- WPF程序将DLL嵌入到EXE的两种方法
WPF程序将DLL嵌入到EXE的两种方法 这一篇可以看作是<Visual Studio 版本转换工具WPF版开源了>的续,关于<Visual Studio 版本转换工具WPF版开源了 ...
- 简要介绍BASE64、MD5、SHA、HMAC几种方法。
加密解密,曾经是我一个毕业设计的重要组件.在工作了多年以后回想当时那个加密.解密算法,实在是太单纯了. 言归正传,这里我们主要描述Java已经实现的一些加密解密算法,最后介绍数字证书. ...
- VB模拟键盘输入的N种方法
VB模拟键盘输入的N种方法http://bbs.csdn.net/topics/90509805hd378发表于: 2006-12-24 14:35:39用VB模拟键盘事件的N种方法 键盘是我们使用计 ...
- 使用C#进行图像处理的几种方法(转)
本文讨论了C#图像处理中Bitmap类.BitmapData类和unsafe代码的使用以及字节对齐问题. Bitmap类 命名空间:System.Drawing 封装 GDI+ 位图,此位图由图形图像 ...
- 第10章 同步设备I/O和异步设备I/O(3)_接收I/O请求完成通知的4种方法
10.5 接收I/O请求完成的通知 (1)I/O请求被加入设备驱动程序的队列,当请求完成以后,设备驱动也要负责通知我们I/O请求己经完成. (2)可以用4种方法来接收I/O请求己经完成的通知 技术 特 ...
随机推荐
- 原:Myeclipse10+Egit+bitbucket实现版本控制
1.首先在https://bitbucket.org注册账号,建立仓库(repository),这部分有问题的可以看https://confluence.atlassian.com/display/B ...
- 【linux】nginx options 跨域问题 请求HTTP错误405 用于访问该页的HTTP动作未被许可 Method Not Allowed
JavaScript JS 跨域问题 HTTP 错误 405 - 用于访问该页的 HTTP 动作未被许可HTTP 错误 405.0 - Method Not Allowed Nginx 处理跨域问题. ...
- 【llinux】yum命令出现Loaded plugins: fastestmirror Determining fastest mirrors
在进行yum 安装的时候.报错了. Loaded plugins: fastestmirror Determining fastest mirrors fastestmirror是yum的一个加速插件 ...
- Android Log缓冲区大小设置
当手机没有连接PC时,手机log缓冲区仍然会保存指定大小的最新log,连接pc,通过adb logcat 仍然可以拿出来 如何查看log缓缓区的大小? 通过adb logcat -g 可以查看 C:\ ...
- Linux命令 uname:查看系统与内核相关信息
zh@zh:~$uname --help zh@zh:~$uname -a //所有系统相关的信息
- LeetCode 45 Jump Game II(按照数组进行移动)
题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十九:SDRAM模块② — 多字读写
实验十九:SDRAM模块② — 多字读写 表示19.1 Mode Register的内容. Mode Register A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A ...
- 一辈子只有1次成为BAT的机会,你如何把握?
本文转自:http://www.fmi.com.cn/index.php?m=content&c=index&a=show&catid=9&id=614308 感谢作者 ...
- TypeScript中处理大数字(会丢失后面部分数字)
为啥要弄这玩意? 最近做数值游戏,需要用到很大的数字,在前端大数字会自动变成e的科学计数法. 有啥问题? 问题: 1. 在传递给服务端时,服务端因为不能处理大数字(怎么就处理不了?!),就想要我传字符 ...
- angular开发控制器之间的通信
一.指令与控制器之间通信,无非是以下几种方法: 基于scope继承的方式 基于event传播的方式 service的方式(单例模式) 二.基于scope继承的方式: 最简单的让控制器之间进行通信的方法 ...