2014-06-20 101 views
1

我經常試圖將ByteArrayOutputStream轉換爲int值。將ByteArrayOutputStream轉換爲int值

我錄製的音頻與麥克風,它就像寫out = new ByteArrayOutputStream()這樣:

out.write(buffer, 0, count); 

byte audio[] = out.toByteArray();

當我打印這我得到這些:[B @ 3456337e

怎麼辦我將它們轉換爲整數。

請幫幫忙,謝謝

+0

你要打印的字節數組,還是真的想將其轉換爲int數組? – kupsef

+0

int array sir @kupsef – user3464035

+0

所以你想要將每個四字節的塊轉換爲一個有符號的整數? –

回答

2

還有就是要做到這一點沒有標準的方法,因爲它實際上取決於你有什麼樣的字節,但是,因爲它是一個音頻源,我覺得你可以做這樣的:

IntBuffer intBuf = 
    ByteBuffer.wrap(byteArray) 
    .order(ByteOrder.BIG_ENDIAN) //or try ByteOrder.LITTLE_ENDIAN 
    .asIntBuffer(); 
int[] array = new int[intBuf.remaining()]; 
intBuf.get(array); 
//The result you want is "array" 

我希望它能幫助你。

+0

感謝您的回覆,對不起,我不明白,我應該打印什麼我的整數數組在這裏?我打印intBuf.get(數組);我得到了java.nio.ByteBufferAsIntBufferB [pos = 11025 lim = 11025 cap = 11025] 我需要的是一個介於0 - 256之間的數字@ user3733356 – user3464035

+0

對不起,我忘了告訴你結果是「數組」 。打印「陣列」,你會得到你想要的 – supermarco

+0

印刷陣列,得到[I @ 629bee3a,我猜是另一個字節 – user3464035

0

將其轉換爲一個數組,包裹陣列中的ByteArrayInputStream,包裹在一個DataInputStream,和使用readInt().

0

請嘗試以下 -

  ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     DataInputStream dataIs = new DataInputStream 
       (new ByteArrayInputStream(out.toByteArray()); 


     // available stream to be read 
     while(dataIs.available()>0) 
     { 

      int k = dataIs.readInt(); 

      // print int 
      System.out.print(k+" "); 
     }