如何用Java读取.EXE文件的内容

发布于 2024-09-30 09:07:53 字数 116 浏览 3 评论 0原文

在 Java 中读取可执行文件的可能选项和最合适的选项是什么。

我想生成 .exe 文件的十六进制表示形式。我正在考虑以二进制方式读取文件,然后进行转换。但是我怎样才能读取.exe呢?

What are the possible options and the most appropiate for reading an executable file in Java.

I want produce the hexadecimal representation of an .exe file. Im thinking of reading the file in binary and then doing the conversion. But how can i read the .exe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

几度春秋 2024-10-07 09:07:53

1)以字节形式读取文件。使用


   BufferedInputStream( new FileInputStream( new File("bin.exe") ) )

2)将每个字节转换为十六进制格式。


    static final String HEXES = "0123456789ABCDEF";
  public static String getHex( byte [] raw ) {
    if ( raw == null ) {
      return null;
    }
    final StringBuilder hex = new StringBuilder( 2 * raw.length );
    for ( final byte b : raw ) {
      hex.append(HEXES.charAt((b & 0xF0) >> 4))
         .append(HEXES.charAt((b & 0x0F)));
    }
    return hex.toString();
  }

1) read the file in as bytes. use


   BufferedInputStream( new FileInputStream( new File("bin.exe") ) )

2) convert each byte to hex format.


    static final String HEXES = "0123456789ABCDEF";
  public static String getHex( byte [] raw ) {
    if ( raw == null ) {
      return null;
    }
    final StringBuilder hex = new StringBuilder( 2 * raw.length );
    for ( final byte b : raw ) {
      hex.append(HEXES.charAt((b & 0xF0) >> 4))
         .append(HEXES.charAt((b & 0x0F)));
    }
    return hex.toString();
  }
羁〃客ぐ 2024-10-07 09:07:53

编辑
我没想到你会想要它作为一个字符串。修改了示例来执行此操作。它的性能应该比使用 BufferedReader 稍好一些,因为我们自己进行缓冲。

public String binaryFileToHexString(final String path)
    throws FileNotFoundException, IOException
{
    final int bufferSize = 512;
    final byte[] buffer = new byte[bufferSize];
    final StringBuilder sb = new StringBuilder();

    // open the file
    FileInputStream stream = new FileInputStream(path);
    int bytesRead;

    // read a block
    while ((bytesRead = stream.read(buffer)) > 0)
    {
        // append the block as hex
        for (int i = 0; i < bytesRead; i++)
        {
            sb.append(String.format("%02X", buffer[i]));
        }
    }
    stream.close();

    return sb.toString();
}

Edit
It didn't occur to me that you'd want it as a string. Modified the example to do so. It should perform slightly better than using a BufferedReader since we're doing the buffering ourselves.

public String binaryFileToHexString(final String path)
    throws FileNotFoundException, IOException
{
    final int bufferSize = 512;
    final byte[] buffer = new byte[bufferSize];
    final StringBuilder sb = new StringBuilder();

    // open the file
    FileInputStream stream = new FileInputStream(path);
    int bytesRead;

    // read a block
    while ((bytesRead = stream.read(buffer)) > 0)
    {
        // append the block as hex
        for (int i = 0; i < bytesRead; i++)
        {
            sb.append(String.format("%02X", buffer[i]));
        }
    }
    stream.close();

    return sb.toString();
}
寻梦旅人 2024-10-07 09:07:53

Java 中的InputStream 是读取二进制文件的主要类。您可以使用 FileInputStream 从文件中读取字节。然后,您可以使用 read() 方法读取每个字节,并根据需要将该字节显示为 2 个十六进制字符。

An InputStream in Java is the primary class for reading binary files. You can use a FileInputStream to read bytes from a file. You could then read in each byte with the read() method and display that byte as 2 hex characters if you wanted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文