二进制文件转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);
	}
}

posted on 2022-12-18 17:12  Milton  阅读(147)  评论(0编辑  收藏  举报

导航