如何最有效地将特定字节从二进制文件转换为字符串

时间:2015-02-04 18:50:28

标签: java string bytearray binary-data

所以我有二进制FRX文件,我需要从中提取字符串到Java。
我把它写入我的Java程序就像这样:

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采用大端格式,而我有小端格式。我显然可以转换,但是在那时我现在的解决方案似乎同样可行。

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

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

3 个答案:

答案 0 :(得分:2)

你可以用任何方式做到这一点,但最简单的可能是。

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

/**
 * 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));
}

答案 1 :(得分:0)

这样的事可能吗?

long length = 0xff && mybytes[0]; length<<8;
length |= 0xff && mybytes[1]; length<<8;
length |= 0xff && mybytes[2]; length<<8;
length |= 0xff && mybytes[3]; length<<8;

答案 2 :(得分:0)

您可以使用您拥有的inputStream作为源,并在需要时创建字符串时使用ByteBuffer来更正字节顺序。这将是最有效的方式。