Question

I'm working on the Java Multimedia IO Project and have run into a problem, when ever I try writing a DWORD it turns out to be 16 bytes in length instead of 4 bytes. I'm using the following code to convert a 4 char string into a 16 bit little endian byte array;

word.getBytes("UTF-32LE")

If some chould please tell me why the array is longer that 4 bytes.

Thanks, Liam.

Was it helpful?

Solution

Because you are encoding into a format (UTF-32) that takes 4 bytes per character See description here

If you want it to be a byte per character use "UTF-8" instead of "UTF-32LE"

OTHER TIPS

Ok, I did some debugging and I found when writing the byte straight from 'word.getBytes("UTF-32LE")' there was an extra 12 bytes being add, for example;

If I was to do "RIFF".getBytes("UTF-32LE") I would get R000I000F000F000 in the file so I used the following bit of code to remove those zeros,

    byte[] bits = word.getBytes("UTF-32LE");
    byte[] dword = new byte[4];
    System.arraycopy(bits, 0, dword, 0, 1);
    System.arraycopy(bits, 4, dword, 1, 1);
    System.arraycopy(bits, 8, dword, 2, 1);
    System.arraycopy(bits, 12, dword, 3, 1);
    write(dword);

I don't know if the problem I was getting was a bug or if that is what its made to return.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top