How to convert specific bytes from binary file into string most efficiently

允我心安 提交于 2020-01-05 10:09:11

问题


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() + " \" ";

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.

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 :)


回答1:


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");
}

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



回答2:


Something like this maybe?

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;



回答3:


You can use the inputStream that you have as the source and use a ByteBuffer to correct the endianess when creating the Strings as needed. This would be the most efficient way.



来源:https://stackoverflow.com/questions/28329098/how-to-convert-specific-bytes-from-binary-file-into-string-most-efficiently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!