简体   繁体   中英

Need help reading bytes from a file (incorrect bytes returned)

i am trying to create a simple program to edit MP3 tags.But the problem is i am stuck at the very first step,as i cannot even pass the file bytes into a byte array.Technically i can,but they are wrong.

For example,if i copy the bytes into a txt file and open it,most of the text is gibberish including the tags(a problem for later),but the first letters are ID3 which are correct.

But if i print the byte array that results from the mp3 in the console,the first values are

1001001 1000100 110011 11 0 0 ..... Which are all invalid characters.But add a zero before the first row,a zero before the second,and TWO zeroes before the third and it now says ID3

What would cause zeroes to get lost like that? It's the same for every mp3 file.Thank you in advance for any help

The piece of code is a very simple copy

try {

            FileInputStream BF1 = new FileInputStream("test.mp3");
            FileOutputStream fout = new FileOutputStream("byteresults.txt");
            byte[] tempbyte = new byte[1024];
            BF1.read(tempbyte);
            BF1.close();





             int i;
             for(i=0;i<900;i++){
             System.out.print(Integer.toBinaryString(tempbyte[i])+'\n');}



        } catch(FileNotFoundException fnf)
        {
                System.out.println("Specified file not found :" + fnf);
        }
        catch(IOException ioe)
        {
                System.out.println("Error while copying file :" + ioe);
        }

When printing Binary format, most systems drop the leading zeroes since they do not contribute to the final value. You only expect eight digits cos it's bytes but binary counting itself doesn't work like that. It doesnt care for 8 slots or 4 slots etc. Consider 3 is written as 11 in binary so why bother printing that as 00000011 ?? Why not 0011 ?? The human reader will ignore those leading zeroes anyway.

Maybe you could try Hex format as it easier (= more efficiency). Something like :

for ( i=0; i<900; i++)
{
    //System.out.print(Integer.toBinaryString(tempbyte[i])+'\n');
    System.out.printf("0x%02X", tempbyte[i]);
}

This way you can even check your output against some Hex Editor software (handling exact same file's bytes). Easier to check that you have the right bytes at right place with right values etc...

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