我是靠谱客的博主 安静小鸽子,最近开发中收集的这篇文章主要介绍二进制文件转Hex和Wav文件转Hex的Java代码二进制文件转HexWav文件转Hex,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
二进制文件转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: %dnn%sn",
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 HznSample width: %d bitsnChannels: %dnSamples: %dnn%sn",
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代码二进制文件转HexWav文件转Hex的全部内容,希望文章能够帮你解决二进制文件转Hex和Wav文件转Hex的Java代码二进制文件转HexWav文件转Hex所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复