二进制文件转Hex

对于需要将二进制数据写入固件的场景(例如mp3文件), 需要将二进制文件表示为byte数组

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. public class RawConverter {
  5. public static final int LINE_LIMIT = 20;
  6. private final String path;
  7. private final int width;
  8. private final boolean littleEnd;
  9. public RawConverter(String path, int width, boolean littleEnd) {
  10. this.path = path;
  11. this.width = width;
  12. this.littleEnd = littleEnd;
  13. }
  14. public static byte[] readBytes(File file)
  15. {
  16. try (FileInputStream fl = new FileInputStream(file)) {
  17. byte[] arr = new byte[(int)file.length()];
  18. fl.read(arr);
  19. return arr;
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. return null;
  23. }
  24. }
  25. public String convert() {
  26. File file = new File(this.path);
  27. if (file.exists()) {
  28. byte[] bytes = readBytes(file);
  29. StringBuilder sb = new StringBuilder();
  30. byte[][] units = new byte[bytes.length/width][width];
  31. for (int i = 0; i < bytes.length; i++) {
  32. int pos = i / width;
  33. int shift = i % width;
  34. if (littleEnd) {
  35. units[pos][width - 1 - shift] = bytes[i];
  36. } else {
  37. units[pos][shift] = bytes[i];
  38. }
  39. }
  40. int count = 0;
  41. for (int i = 0; i < units.length; i++) {
  42. sb.append("0x");
  43. for (int j = 0; j < width; j++) {
  44. sb.append(String.format("%02x", units[i][j]));
  45. }
  46. if (i < units.length - 1) {
  47. sb.append(", ");
  48. }
  49. count++;
  50. if (count % LINE_LIMIT == 0) {
  51. sb.append("\n");
  52. count = 0;
  53. }
  54. }
  55. return String.format("Samples: %d\n\n%s\n",
  56. bytes.length / width,
  57. sb);
  58. }
  59. return null;
  60. }
  61. public static void main(String[] args) {
  62. String path = "/home/user/Song-4-clip4.mp3";
  63. String output = new RawConverter(path, 1, true).convert();
  64. System.out.println(output);
  65. }
  66. }

Wav文件转Hex

  1. import org.apache.commons.io.IOUtils;
  2. import javax.sound.sampled.AudioFormat;
  3. import javax.sound.sampled.AudioInputStream;
  4. import javax.sound.sampled.AudioSystem;
  5. import java.net.URL;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. public class Converter {
  9. public static final int LINE_LIMIT = 20;
  10. private final String path;
  11. private final int width;
  12. public Converter(String path, int width) {
  13. this.path = path;
  14. this.width = width;
  15. }
  16. public String convert() {
  17. try {
  18. final Path path = Paths.get(this.path);
  19. final URL url = path.toUri().toURL();
  20. final AudioInputStream ais = AudioSystem.getAudioInputStream(url);
  21. final AudioFormat format = ais.getFormat();
  22. byte[] bytes = IOUtils.toByteArray(ais);
  23. StringBuilder sb = new StringBuilder();
  24. int wc = 0, count = 0;
  25. for (int i = 0; i < bytes.length; i++) {
  26. if (wc == 0) {
  27. sb.append("0x");
  28. }
  29. sb.append(String.format("%02x", bytes[i]));
  30. wc++;
  31. if (wc % width == 0 && i < bytes.length - 1) {
  32. sb.append(", ");
  33. wc = 0;
  34. count++;
  35. }
  36. if (wc == 0 && count % LINE_LIMIT == 0) {
  37. sb.append("\n");
  38. count = 0;
  39. }
  40. }
  41. return String.format("Sample rate: %.2f Hz\nSample width: %d bits\nChannels: %d\nSamples: %d\n\n%s\n",
  42. format.getSampleRate(),
  43. format.getSampleSizeInBits(),
  44. format.getChannels(),
  45. bytes.length / width,
  46. sb);
  47. } catch (Throwable t) {
  48. throw new RuntimeException(t);
  49. }
  50. }
  51. public static void main(String[] args) {
  52. String path = "/home/user/627b.wav";
  53. String output = new Converter(path, 2).convert();
  54. System.out.println(output);
  55. }
  56. }

二进制文件转Hex和Wav文件转Hex的Java代码的更多相关文章

  1. 【Java学习day04】Java文件的创建和Java代码的执行

    Java文件的创建和Java代码的执行 随便新建一个文件夹,存放代码 在新建的文件夹里新建一个java文件 新建一个文本文档 将新建的文本文档重命名为hello.java 注意了!后缀必须改为.jav ...

  2. Linux音频编程--使用ALSA库播放wav文件

    在UBUNTU系统上使用alsa库完成了对外播放的wav文件的案例. 案例代码: /** *test.c * *注意:这个例子在Ubuntu 12.04.1环境下编译运行成功. * */ #inclu ...

  3. HEX和BIN文件的区别

    以下的内容是从网上转载来的,原文地址:http://blog.csdn.net/zhangliang_571/article/details/8519469  在这里感谢原作者. 1,是在keil中编 ...

  4. 豹哥嵌入式讲堂:ARM Cortex-M开发之文件详解(8)- 镜像文件(.bin/.hex/.s19)

    大家好,我是豹哥,猎豹的豹,犀利哥的哥.今天豹哥给大家讲的是嵌入式开发里的image文件(.bin, .hex, .s19). 今天这节课是豹哥<ARM Cortex-M开发之文件详解>主 ...

  5. 痞子衡嵌入式:ARM Cortex-M文件那些事(8)- 镜像文件(.bin/.hex/.s19)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是嵌入式开发里的image文件(.bin, .hex, .s19). 今天这节课是痞子衡<ARM Cortex-M文件那些事>主 ...

  6. hex转mif文件 verilog

    用FPGA来跑ARM 核的时候,刚开始将Keil编译产生的hex文件拿来仿真和下到板子上的时候,发现程序运行不正确.细细观察仿真波形发现,在Altera的ROM IP中直接调用Keil产生的hex文件 ...

  7. C#读取wav文件

    private void showWAVForm(string filepath) //此函数只能用于读取16bit量化单声道的WAV文件 { FileStream fs = new FileStre ...

  8. delphi 合并两个 Wav 文件流的函数

    合并两个 Wav 文件的函数 实例一 unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphic ...

  9. wav文件系列_1_wav格式解读

    本文介绍 wav 文件格式,主要关注该类格式的结构. 参考: [1] 以一个wav文件为实例分析wav文件格式 ( 2017.04.11 CSDN ) [2] WAV ( Wikipedia ) [3 ...

  10. C++标准库实现WAV文件读写

    在上一篇文章RIFF和WAVE音频文件格式中对WAV的文件格式做了介绍,本文将使用标准C++库实现对数据为PCM格式的WAV文件的读写操作,只使用标准C++库函数,不依赖于其他的库. WAV文件结构 ...

随机推荐

  1. DC-实验

    设置及综合流程

  2. 274. H 指数

    1.题目介绍 给你一个整数数组 citations ,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数.计算并返回该研究者的 h 指数. 根据维基百科上 h 指数的定义:h 代表 ...

  3. crypto常用算法

    欧几里得算法(辗转相除法) def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) 扩展欧几里得算法 def ext_euclid( ...

  4. 【转帖】ESXi 6.x 安装storcli监控raid卡状态

    https://b2b.baidu.com/land?id=744541c6188f7937d6dc97d6fb9142ff10 脚本宝典收集整理的这篇文章主要介绍了ESXi 6.x 安装storcl ...

  5. 【转帖】Seccomp、BPF与容器安全

    语音阅读2022-06-30 20:26 本文详细介绍了关于seccomp的相关概念,包括seccomp的发展历史.Seccomp BPF的实现原理已经与seccomp相关的一些工具等.此外,通过实例 ...

  6. [转帖]CPU计算性能speccpu2006的测试方法及工具下载

    https://www.yii666.com/blog/335517.html CPU计算性能speccpu2006的测试方法及工具下载 简介 测试原理 目录结构 测试方法 基准测试项解析 测试结果 ...

  7. 【验证码逆向专栏】最新某验三代滑块逆向分析,干掉所有的 w 参数!

    声明 本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容.敏感网址.数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关! 本文章未经许 ...

  8. vue 进阶学习(二):node.js、npm、webpack、vue-cli

    node.js.npm.webpack.vue-cli 前言:主要对插件的描述,安装,卸载.使用以及注意点 1 node.js 说明:是一个基于 Chrome V8 引擎的 JavaScript 运行 ...

  9. 精进语言模型:探索LLM Training微调与奖励模型技术的新途径

    精进语言模型:探索LLM Training微调与奖励模型技术的新途径 LLMs Trainer 是一个旨在帮助人们从零开始训练大模型的仓库,该仓库最早参考自 Open-Llama,并在其基础上进行扩充 ...

  10. Python 解析JSON实现主机管理

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它以易于阅读和编写的文本形式表示数据.JSON 是一种独立于编程语言的数据格式,因此在不同的编程语言中都有对 ...