简体   繁体   English

需要帮助从文件读取字节(返回错误的字节)

[英]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. 我试图创建一个简单的程序来编辑MP3标签。但是问题是我停留在第一步,因为我什至不能将文件字节传递到字节数组中。从技术上讲我可以,但是它们是错误的。

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. 例如,如果我将字节复制到一个txt文件中并打开它,则大多数文本都是乱码,包括标签(稍后会出现问题),但第一个字母是ID3,这是正确的。

But if i print the byte array that results from the mp3 in the console,the first values are 但是如果我在控制台中打印由mp3产生的字节数组,则第一个值是

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 1001001 1000100 110011 11 0 0 .....都是无效字符。但是在第一行之前添加一个零,在第二行之前添加一个零,在第三行之前添加两个零,现在显示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 每个mp3文件都一样。在此先感谢您的帮助

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. 您只希望它的字节为8位cos,但是二进制计数本身却不能那样工作。 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 ?? 它不在乎8个插槽或4个插槽等。考虑3以二进制形式写为11 ,所以为什么要麻烦地将其打印为00000011 Why not 0011 ?? 为什么不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). 这样,您甚至可以对照某些Hex Editor软件检查输出(处理完全相同的文件字节)。 Easier to check that you have the right bytes at right place with right values etc... 更容易检查您在正确的位置使用正确的值等保存了正确的字节...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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