gpt4 book ai didi

java - 在 Java 中将十六进制文件转换为 unsigned int 32 Big-Endian

转载 作者:行者123 更新时间:2023-11-30 06:18:37 26 4
gpt4 key购买 nike

这可能是一个已经问过的问题,但我还没有找到满意的答案。特别是因为这种转换一直是用 C 或 C++ 完成的。

顺便说一句,如何将一个十六进制文件 (200MB) 转换成 Java 中的 UINT32 大端表示形式?

这是我正在努力实现的一个例子:

54 00 00 00  -> 84       
55 F1 2E 04 -> 70185301
A2 3F 32 01 -> 20070306
and so on

编辑

File fileInputString = new File(inputFileField.getText());
FileInputStream fin = new FileInputStream(fileInputString);
FileOutputStream out = new FileOutputStream(fileDirectoryFolder.getText() +"/"+ fileInputString.getName());
byte[] fileContent = new byte[(int)fileInputString.length()];
fin.read(fileContent);
System.out.println("File Lenght" + fileContent.length);
for(int i = 0; i < fileContent.length; i++){
Byte b = fileContent[i]; // Boxing conversion converts `byte` to `Byte`
int value = b.intValue();
out.write(value);
}
close();
System.out.println("Done");

编辑 2

File fileInputString = new File(inputFileField.getText());
FileInputStream fin = new FileInputStream(fileInputString);
FileOutputStream out = new FileOutputStream(fileDirectoryFolder.getText() +"/"+ fileInputString.getName());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] fileContent = new byte[(int)fileInputString.length()];
System.out.println("File Lenght" + fileContent.length);
int bytesRead;
while (( bytesRead = fin.read(fileContent)) != -1) {
ByteBuffer.wrap(fileContent).order(ByteOrder.LITTLE_ENDIAN).getLong();
bos.write(fileContent, 0, bytesRead);
}
out.write(bos.toByteArray());
System.out.println("Done");

编辑 3

DataOutputStream out = new DataOutputStream(new FileOutputStream(output)); 
DataInputStream in = new DataInputStream(new FileInputStream(input))) {
int count = 0;

while (count < input.length() - 4) {
in.readFully(buffer, 4, 4);
String s=Long.toString(ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).getLong());
out.writeBytes( s + " ");
count += 4;
}

谢谢

最佳答案

下面的代码应该足够了。它使用 long 值来确保我们可以完全表示四个字节可以表示的正值范围。

注意:此代码假定十六进制输入为四个字节。您可能希望在生产代码中添加更多检查和措施。

private static long toLong(String hex) {
hex = hex.replace(" ", "") + "00000000";
byte[] data = DatatypeConverter.parseHexBinary(hex);
return ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).getLong();
}

public static void main(String[] args) throws Exception {
System.out.println(toLong("54 00 00 00"));
System.out.println(toLong("55 F1 2E 04"));
System.out.println(toLong("A2 3F 32 01"));
System.out.println(toLong("FF FF FF FF"));
}

输出:

8470185301200703064294967295

Based on your recent edits, I propose some code such as the following. Note that it assumes your input is a multiple of four bytes in length. Any left-over bytes are ignored:

File input = new File("whatever");

byte[] buffer = new byte[8];
List<Long> result = new ArrayList<>();

try (DataInputStream in = new DataInputStream(new FileInputStream(input))) {
int count = 0;

// Note: any trailing bytes are ignored
while (count < input.length() - 4) {
in.readFully(buffer, 4, 4);
result.add(ByteBuffer.wrap(buffer)
.order(ByteOrder.LITTLE_ENDIAN).getLong());
count += 4;
}
}

关于java - 在 Java 中将十六进制文件转换为 unsigned int 32 Big-Endian,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24280999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com