import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
* 文件,byte[],HexString 之间的转换
*
*/
public class FileDataConvert {
public static void main(String[] args) {
String srcFilePath = "d:/testFrom.png";
String outFilePath = "d:/";
String outFileName = "testTo.png";
byte[] bytes = getBytesFromFile(srcFilePath);
String str = bytes2HexString(bytes);
System.out.println(str);
byte[] bytes2 = hexString2Bytes(str);
saveBytes2File(bytes2, outFilePath, outFileName);
}
/**
* 从文件中获取byte数组
*/
public static byte[] getBytesFromFile(String filePath) {
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer;
}
/**
* 根据byte数组生成文件
*/
public static void saveBytes2File(byte[] bfile, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory()) {
dir.mkdirs();
}
file = new File(filePath + "\\" + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
/**
* 从字节数组到十六进制字符串转换
*/
public static String bytes2HexString(byte[] b) {
byte[] buff = new byte[2 * b.length];
for (int i = 0; i < b.length; i++) {
buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
buff[2 * i + 1] = hex[b[i] & 0x0f];
}
return new String(buff);
}
/**
* 从十六进制字符串到字节数组转换
*/
public static byte[] hexString2Bytes(String hexstr) {
byte[] b = new byte[hexstr.length() / 2];
int j = 0;
for (int i = 0; i < b.length; i++) {
char c0 = hexstr.charAt(j++);
char c1 = hexstr.charAt(j++);
b[i] = (byte) ((parse(c0) << 4) | parse(c1));
}
return b;
}
private final static byte[] hex = "0123456789ABCDEF".getBytes();
private static int parse(char c) {
if (c >= 'a')
return (c - 'a' + 10) & 0x0f;
if (c >= 'A')
return (c - 'A' + 10) & 0x0f;
return (c - '0') & 0x0f;
}
}
分享到:
相关推荐
当我们使用蓝牙通信或者其他通信技术,常常需要用到数据之间的转换,此工具则是java收发蓝牙数据间的hexString 与byte之间的相互转化
1.字符串(String)与Byte[] 的互相转换. 2.Hex(String形式)与Byte[] 的互相转换.
测试时通常会用到一些hexstring参数,而在传递时需要转换成Base64String。这个小程序,能够很好的将HexString和Base64String相互转换。
提供两个exe软件 一个国内的一个国外的 国外的使用方法:在命令行中输入 软件名 需要转换文件名 转换后文件名
hex2byte byte2hex,转换成字符串传输
软件介绍: Hex2Mif用于将Intel Hex文件转换为Mif文件,可选择8位RAM/16位RAM,8位ROM或32位ROM,实时显示转换状态。
hex文件与bin文件互相转换工具,hex文件合并工具,hex2bin.py源码
将HexString数据转换为文本并返回。 十六进制字符串模式: =================== 放置在“十六进制”部分的十六进制数字序列可以转换为Unicode字符“十六进制 - >字符串”选项。在这种模式下,首先将数据转换为字节,...
.OUT文件转换为.HEX文件方法 1. 工具 TI公司提供的hex2000软件 2. 步骤 举例说明将 Powervotex305KTL_MasterV1.0.2.out 转换成Powervortex305KTL_Master_ V1.0.2.hex文件 2.1复制出hex2000,并与xxx.out文件放到同一...
采用C#编写的可以将HEX文件转换为文本内容输出,并带有串口功能,串口发送数据可以根据自己的需要添加协议
hex和bin相互转换工具。里面有三个文件:hex2bin.exe 、bin2hex.exe、hex_bin
很多朋友不清楚如何在Byte[]和String之间进行转换?下面小编给大家带来了byte与string转换的方法,感兴趣的朋友参考下吧
字符串到16进制字符串之间的相互转换,能在通信程序中有很好的应用
采用C#语言编写,VS2010工具编译,可以将.hex文件转换成.txt文件,将.hex文件中的数据提取到.txt文件中,每行显示一个数据,可选择1byte/2byte/4byte转换方式。
CCS中发布版本时,如何将生成的.out文件转换成最终的hex文件。
如何将elf文件转换为hex文件 elf(Executable and Linkable Format)文件一般是由gcc编译器生成的,在Linux开发环境使用较多,但Windows一般情况下需要使用hex文件来进行烧录,那么如何将elf格式转换为hex格式呢?...
主要介绍了Java中byte[]、String、Hex字符串等转换的方法,代码很简单,需要的朋友可以参考下
2.hex转换成bin文件使用方法直接把hex文件拖到HEX2BIN.EXE软件上自动生成bin文件 HEX 转 BIN 程序,弄过单片机的人可能都知道,很多编译器都会编译出一种叫HEX 的文件,这好像是Intel定的一个格式,就是把二进制的...
将elf文件转换为hex文件的小程序,使用前安装arm-none-eabi编译工具链,复制到elf文件所在目录双击执行,若当前文件没有.elf文件,则会提示 No such file,若转换成功则会在目录内生成HexFile.hex文件
分享我日常工作中在windows环境下使用的一款非常好用的bin文件和hex文件相互转换的工具,bin转hex时可以设定起始地址。