java - 与 C# 相比,从 Java 获取错误字节

标签 java c# binary byte fileinputstream

所以我有一些 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;
  }

请尝试查看下面的帖子。

Converting Little Endian to Big Endian

关于java - 与 C# 相比,从 Java 获取错误字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28153951/

相关文章:

c# - 使用工作目录的相对路径并在 C# 项目中启动

方法签名中的 Java 泛型类型不匹配

java - Apache 点燃: How can I improve insertion performance?

c# - 为什么 C# Struct 与属性的行为不同? (与声明为字段相比)

c# - 在 PropertyGrid 的集合编辑器中更改类型名称

binary - 这是什么浮点格式?

十六进制数可以用十进制数加减吗?

r - 在 R 中写入二进制文件

java - IIS 在性能和可伸缩性方面与企业 Web 应用程序的 weblogic 和 websphere 相比如何?

java - 使用 Hibernate 进行分层数据版本控制的好的数据库设计是什么?