简体   繁体   中英

Reading a String from Binary File returns nothing

I try to program a simple game (but my question is not so game specific) so i wrote a little MapLoader . At the moment it only reads the File Header and prints it on the Screen.

Info:

  • Signature is a byte
  • Version is an int
  • length is a long
  • name is a 32 byte long String

Also, all these are signed as there is no unsigned in Java.

However i have a function toString(byte[] array) . Its implemented like this:

private String toString(byte[] array) {
    char[] buffer = new char[array.length];
    StringBuilder builder = new StringBuilder(array.length);

    for(int i = 0; i < array.length; i++) {
        buffer[i] = (char) array[i];
        builder.append(buffer[i]);
    }
    return builder.toString();
}

I pass in an sub-array of data[] (holding all data)

header.name = toString(Arrays.copyOfRange(data, 11, 32));

But when doing

Debugger.debug("Signatur: " +  header.signature);
Debugger.debug("Version: " + header.version);
Debugger.debug("Länge: " + header.length);
Debugger.debug("Name: " + header.name);

I only get:

Signatur: 86
Version: 1
Länge: 14
Name: 

My Map File looks like this:

56 00 00 00 01 00 00 00 00 00 00 00 0E 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 41 0A     

Use one of the java.lang.String constructors that takes a byte array as a parameter, rather than rolling your own. The String implementation deals properly with all the character set issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM