2012-05-05 69 views
-2

我想寫一個字節數組到文件。這是代碼的網頁http://allmybrain.com/2012/03/16/quick-convert-raw-g711-ulaw-audio-to-a-au-file/如何將字節數組寫入Java文件

import struct 
    header = [ 0x2e736e64, 24, 0xffffffff, 1, 8000, 1 ] 
    o=open('out.au','wb') 
    o.write (struct.pack (">IIIIII", *header)) 
    raw = open('in.raw','rb').read() 
    o.write(raw) 
    o.close() 

我轉換爲Java:

  byte [] header= { 0x2e736e64, 24, 0xffffffff, 1, 8000, 1 }; 
     FileOutputStream out = new FileOutputStream(file); 
     out.write(header); 

但它是錯誤。你能幫我解決它嗎?謝謝

+0

您需要指定錯誤是什麼。 –

+0

提示:0x2e736e64不是一個字節的有效值... –

+0

是的,我想寫0x2e736e64作爲字節數組。我使用(字節)0x2e736e64。但是數據丟失了。 ASCII是「.snd」 – john2182

回答

5

在你的Python代碼中,你寫的是32位整數而不是8位字節; 您可以使用此代碼具有相同的結果:

byte [] header= { 0x2e, 0x73, 0x6e, 0x64, 
         0x0, 0x0, 0x0, 24, 
         -1, -1, -1, -1, 
         0, 0,  0, 1, 
         0, 0,  0x1f, 0x40, 
         0, 0,  0, 1 }; 
FileOutputStream out = new FileOutputStream(file); 
out.write(header); 
+1

非常感謝。 – john2182