如何最有效地将特定字节从二进制文件转换为字符串 [英] How to convert specific bytes from binary file into string most efficiently

查看:215
本文介绍了如何最有效地将特定字节从二进制文件转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有二进制FRX文件,我需要从中将字符串提取到Java中.
我是这样写到我的Java程序中的:

So I have binary FRX files, from which I need to extract strings into Java.
I wrote this into my Java program like so:

FileInputStream ReadFRX = null ;
FileOutputStream TempCapt = null ;
    try{                
        // refNum is hex number on end of VB form property converted to decimal, ex: $"frmResidency.frx":0134
        int refNum = Integer.parseInt(line.substring(line.length() - 4, line.length()), 16);

        // FRXtemp.txt is created, to temporarily write FRX captions onto to be read from. 
        PrintWriter writer = new PrintWriter("FRXtemp.txt", "UTF-8");
        writer.close();

        //opens corresponding FRX file to read into
        ReadFRX = new FileInputStream("FRXFiles\\"+curFrmName + ".frx");
        //aLittleEndian... must be used to match readInt() little-endianness
        LittleEndianDataInputStream ActReadFRX = new LittleEndianDataInputStream(ReadFRX);
        TempCapt = new FileOutputStream("FRXtemp.txt");

        ActReadFRX.skipBytes(refNum);
        int length = ActReadFRX.readInt();
        int c;

            for (c = 0; c < length; c++) {
                // first read byte and check for EOF
                TempCapt.write(ActReadFRX.read());
            }
        }
//If caption is not read properly (ie. possibly wrong bytes), EOF Exception will occur and designer will break
catch (EOFException e){

    System.out.println("ERROR : FRX Caption property was mishandled");
    break;
}

//Read data from FRXtemp.txt into string
String actCaption = "\"" + new Scanner(new File("FRXtemp.txt")).useDelimiter("\\A").next() + " \" ";

这很好用,但是我认为写一个临时文件非常必要,这样我就可以读出它了.

为什么我没想到更有效的方法:
我觉得更实用的方法是使用Byte[] Array,然后将其转换为字符串,但是我必须仅具有存储字符串的字节.研究使我相信RandomAccessFile才是必需的,这样我可以设置与ReadInt的偏移量以开始读取字节,但是RandomAccessFile假定为大端格式,而我为小端格式.我显然可以转换,但是到那时我当前的解决方案似乎同样可行.

This works perfectly, however I think writing to a temporary file so that I can read off of it must be highly unnecessary.

Why I can't think of a more efficient method:
I feel like a much more practical approach would be to use a Byte[] Array, and then convert that to a string, however I must only have the bytes in which the string are stored. Research led me to believe that RandomAccessFile was then necessary so that I could set an offset from ReadInt to begin reading bytes , however RandomAccessFile assumes big-endian format, whereas I have little-endian format. I can obviously convert, however at that point my current solution seems just as viable.

我的问题是 ,是否存在一种有效的方法来转换对应于4字节整数的特定字节部分(从具有Little-Endian格式的二进制文件中转换) )转换为Java中的字符串?

我觉得好像我必须忽略一些更简单的事情.谢谢:)

My question is, is there an efficient way to convert a specific section of bytes corresponding to a 4-byte integer (from a binary file with little-endian format) into a string in Java?

I feel as though I must be overlooking something much more simple. Thanks :)

推荐答案

您可以通过多种方式进行此操作,但是最简单的方法可能是

You can do this any number ways, however the simplest might be.

try (DataInputStream dis = new DataInputStream(new FileInputStream(file))) {
    dis.skip(bytesToSkip);
    int length = Integer.reverseBytes(dis.readInt());
    byte[] bytes = new bytes[length];
    dis.readFully(bytes);
    return new String(bytes, "UTF-8");
}

您可能一直在寻找的方法在Integer

The method you might have been looking for is in Integer

/**
 * Returns the value obtained by reversing the order of the bytes in the
 * two's complement representation of the specified {@code int} value.
 *
 * @param i the value whose bytes are to be reversed
 * @return the value obtained by reversing the bytes in the specified
 *     {@code int} value.
 * @since 1.5
 */
public static int reverseBytes(int i) {
    return ((i >>> 24)           ) |
           ((i >>   8) &   0xFF00) |
           ((i <<   8) & 0xFF0000) |
           ((i << 24));
}

这篇关于如何最有效地将特定字节从二进制文件转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆