二进制文件转Hex和Wav文件转Hex的Java代码
二进制文件转Hex
对于需要将二进制数据写入固件的场景(例如mp3文件), 需要将二进制文件表示为byte数组
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class RawConverter {
public static final int LINE_LIMIT = 20;
private final String path;
private final int width;
private final boolean littleEnd;
public RawConverter(String path, int width, boolean littleEnd) {
this.path = path;
this.width = width;
this.littleEnd = littleEnd;
}
public static byte[] readBytes(File file)
{
try (FileInputStream fl = new FileInputStream(file)) {
byte[] arr = new byte[(int)file.length()];
fl.read(arr);
return arr;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public String convert() {
File file = new File(this.path);
if (file.exists()) {
byte[] bytes = readBytes(file);
StringBuilder sb = new StringBuilder();
byte[][] units = new byte[bytes.length/width][width];
for (int i = 0; i < bytes.length; i++) {
int pos = i / width;
int shift = i % width;
if (littleEnd) {
units[pos][width - 1 - shift] = bytes[i];
} else {
units[pos][shift] = bytes[i];
}
}
int count = 0;
for (int i = 0; i < units.length; i++) {
sb.append("0x");
for (int j = 0; j < width; j++) {
sb.append(String.format("%02x", units[i][j]));
}
if (i < units.length - 1) {
sb.append(", ");
}
count++;
if (count % LINE_LIMIT == 0) {
sb.append("\n");
count = 0;
}
}
return String.format("Samples: %d\n\n%s\n",
bytes.length / width,
sb);
}
return null;
}
public static void main(String[] args) {
String path = "/home/user/Song-4-clip4.mp3";
String output = new RawConverter(path, 1, true).convert();
System.out.println(output);
}
}
Wav文件转Hex
import org.apache.commons.io.IOUtils;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Converter {
public static final int LINE_LIMIT = 20;
private final String path;
private final int width;
public Converter(String path, int width) {
this.path = path;
this.width = width;
}
public String convert() {
try {
final Path path = Paths.get(this.path);
final URL url = path.toUri().toURL();
final AudioInputStream ais = AudioSystem.getAudioInputStream(url);
final AudioFormat format = ais.getFormat();
byte[] bytes = IOUtils.toByteArray(ais);
StringBuilder sb = new StringBuilder();
int wc = 0, count = 0;
for (int i = 0; i < bytes.length; i++) {
if (wc == 0) {
sb.append("0x");
}
sb.append(String.format("%02x", bytes[i]));
wc++;
if (wc % width == 0 && i < bytes.length - 1) {
sb.append(", ");
wc = 0;
count++;
}
if (wc == 0 && count % LINE_LIMIT == 0) {
sb.append("\n");
count = 0;
}
}
return String.format("Sample rate: %.2f Hz\nSample width: %d bits\nChannels: %d\nSamples: %d\n\n%s\n",
format.getSampleRate(),
format.getSampleSizeInBits(),
format.getChannels(),
bytes.length / width,
sb);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
public static void main(String[] args) {
String path = "/home/user/627b.wav";
String output = new Converter(path, 2).convert();
System.out.println(output);
}
}
二进制文件转Hex和Wav文件转Hex的Java代码的更多相关文章
- 【Java学习day04】Java文件的创建和Java代码的执行
Java文件的创建和Java代码的执行 随便新建一个文件夹,存放代码 在新建的文件夹里新建一个java文件 新建一个文本文档 将新建的文本文档重命名为hello.java 注意了!后缀必须改为.jav ...
- Linux音频编程--使用ALSA库播放wav文件
在UBUNTU系统上使用alsa库完成了对外播放的wav文件的案例. 案例代码: /** *test.c * *注意:这个例子在Ubuntu 12.04.1环境下编译运行成功. * */ #inclu ...
- HEX和BIN文件的区别
以下的内容是从网上转载来的,原文地址:http://blog.csdn.net/zhangliang_571/article/details/8519469 在这里感谢原作者. 1,是在keil中编 ...
- 豹哥嵌入式讲堂:ARM Cortex-M开发之文件详解(8)- 镜像文件(.bin/.hex/.s19)
大家好,我是豹哥,猎豹的豹,犀利哥的哥.今天豹哥给大家讲的是嵌入式开发里的image文件(.bin, .hex, .s19). 今天这节课是豹哥<ARM Cortex-M开发之文件详解>主 ...
- 痞子衡嵌入式:ARM Cortex-M文件那些事(8)- 镜像文件(.bin/.hex/.s19)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是嵌入式开发里的image文件(.bin, .hex, .s19). 今天这节课是痞子衡<ARM Cortex-M文件那些事>主 ...
- hex转mif文件 verilog
用FPGA来跑ARM 核的时候,刚开始将Keil编译产生的hex文件拿来仿真和下到板子上的时候,发现程序运行不正确.细细观察仿真波形发现,在Altera的ROM IP中直接调用Keil产生的hex文件 ...
- C#读取wav文件
private void showWAVForm(string filepath) //此函数只能用于读取16bit量化单声道的WAV文件 { FileStream fs = new FileStre ...
- delphi 合并两个 Wav 文件流的函数
合并两个 Wav 文件的函数 实例一 unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphic ...
- wav文件系列_1_wav格式解读
本文介绍 wav 文件格式,主要关注该类格式的结构. 参考: [1] 以一个wav文件为实例分析wav文件格式 ( 2017.04.11 CSDN ) [2] WAV ( Wikipedia ) [3 ...
- C++标准库实现WAV文件读写
在上一篇文章RIFF和WAVE音频文件格式中对WAV的文件格式做了介绍,本文将使用标准C++库实现对数据为PCM格式的WAV文件的读写操作,只使用标准C++库函数,不依赖于其他的库. WAV文件结构 ...
随机推荐
- DC-实验
设置及综合流程
- 274. H 指数
1.题目介绍 给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数.计算并返回该研究者的 h 指数. 根据维基百科上 h 指数的定义:h 代表 ...
- crypto常用算法
欧几里得算法(辗转相除法) def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) 扩展欧几里得算法 def ext_euclid( ...
- 【转帖】ESXi 6.x 安装storcli监控raid卡状态
https://b2b.baidu.com/land?id=744541c6188f7937d6dc97d6fb9142ff10 脚本宝典收集整理的这篇文章主要介绍了ESXi 6.x 安装storcl ...
- 【转帖】Seccomp、BPF与容器安全
语音阅读2022-06-30 20:26 本文详细介绍了关于seccomp的相关概念,包括seccomp的发展历史.Seccomp BPF的实现原理已经与seccomp相关的一些工具等.此外,通过实例 ...
- [转帖]CPU计算性能speccpu2006的测试方法及工具下载
https://www.yii666.com/blog/335517.html CPU计算性能speccpu2006的测试方法及工具下载 简介 测试原理 目录结构 测试方法 基准测试项解析 测试结果 ...
- 【验证码逆向专栏】最新某验三代滑块逆向分析,干掉所有的 w 参数!
声明 本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容.敏感网址.数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关! 本文章未经许 ...
- vue 进阶学习(二):node.js、npm、webpack、vue-cli
node.js.npm.webpack.vue-cli 前言:主要对插件的描述,安装,卸载.使用以及注意点 1 node.js 说明:是一个基于 Chrome V8 引擎的 JavaScript 运行 ...
- 精进语言模型:探索LLM Training微调与奖励模型技术的新途径
精进语言模型:探索LLM Training微调与奖励模型技术的新途径 LLMs Trainer 是一个旨在帮助人们从零开始训练大模型的仓库,该仓库最早参考自 Open-Llama,并在其基础上进行扩充 ...
- Python 解析JSON实现主机管理
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它以易于阅读和编写的文本形式表示数据.JSON 是一种独立于编程语言的数据格式,因此在不同的编程语言中都有对 ...