所以我有一些 FRX 二进制文件,我试图使用 Java 的二进制读取方法从中获取字符串标题。
我能够这样做,并使用以下程序在 C# 中指定读取字节的区域:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
public class GetFromFRX
{
public static void Main()
{
StringBuilder buffer = new StringBuilder();
using (BinaryReader b = new BinaryReader(File.Open("frmResidency.frx", FileMode.Open)))
{
try
{
b.BaseStream.Seek(641, SeekOrigin.Begin);
int length = b.ReadInt32();
for (int i = 0; i < length; i++)
{
buffer.Append(b.ReadChar());
}
}
catch (Exception e)
{
Console.WriteLine( "Error obtaining resource\n" + e.Message);
}
}
Console.WriteLine(buffer);
}
}
问题更新:
尝试在 Java 中做同样的事情,我构建了以下程序。现在我已经实现了 Guava 以便使用 LittleEndian
等价物,但是现在我的长度打印为 24,结果我只得到输出文件中的前 24 个字节。 ReadInt
是否不适合这种情况,并且其功能与 ReadInt32
不同?
import java.io.*;
import com.google.common.io.*;
public class RealJavaByteReader {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("frmResidency.frx");
LittleEndianDataInputStream din = new LittleEndianDataInputStream(in);
out = new FileOutputStream("output.txt");
int length = din.readInt();
System.out.println(length);
int c;
for (c = 0; c < length; c++) {
// TODO: first read byte and check for EOF
out.write(din.read());
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
最佳答案
Elizion,
这可能是因为您可能正在读取使用小端存储的 int。因此,Java 使用 Big endian 和 .NET little endian。
使用下面的函数将 little endian int 转换为 java 中的 big endian int。
/**
* Byte swap a single int value.
*
* @param value Value to byte swap.
* @return Byte swapped representation.
*/
public static int swap (int value)
{
int b1 = (value >> 0) & 0xff;
int b2 = (value >> 8) & 0xff;
int b3 = (value >> 16) & 0xff;
int b4 = (value >> 24) & 0xff;
return b1 << 24 | b2 << 16 | b3 << 8 | b4 << 0;
}
请尝试查看下面的帖子。
关于java - 与 C# 相比,从 Java 获取错误字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28153951/