【问题标题】:Convert ByteArrayOutputStream to int values将 ByteArrayOutputStream 转换为 int 值
【发布时间】:2014-06-20 09:38:51
【问题描述】:

我一直在尝试将 ByteArrayOutputStream 转换为 int 值。

我正在用麦克风录制音频并将其写入out = new ByteArrayOutputStream(),如下所示:

out.write(buffer, 0, count);

byte audio[] = out.toByteArray();

当我打印这个时,我得到了这些:[B@3456337e

如何将这些转换为整数。

请帮忙,谢谢

【问题讨论】:

  • 你想打印字节数组,还是真的想把它转换成int数组?
  • int 数组先生@kupsef
  • 那么你想把每一个4字节的块转换成有符号整数吗?

标签: java android bytearrayoutputstream


【解决方案1】:

没有标准的方法,因为实际上它取决于你拥有什么样的字节,但是,因为它是一个音频源,我认为你可以这样做:

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"

希望对你有帮助。

【讨论】:

  • 感谢您的回复,抱歉我没听明白,我应该打印什么来获取我的整数数组?我打印了 intBuf.get(array);我得到了 java.nio.ByteBufferAsIntBufferB[pos=11025 lim=11025 cap=11025] 我需要的是 0 - 256 @user3733356 之间的单个数字
  • 是的,对不起,我忘了告诉你结果是“数组”。打印“数组”,你就会得到你想要的
  • 打印数组,得到[I@629bee3a,我猜是另一个字节
  • @user3464035 试试System.out.println(Arrays.toString(array));。
  • @Duncan 我得到一堆 0,0,0,0,0,0 因为音频在波动,我应该得到像 100、80、34、...这样的数字
【解决方案2】:

将其转换为数组,将数组包装在 ByteArrayInputStream 中,将其包装在 DataInputStream, 中并使用 readInt().

【讨论】:

    【解决方案3】:

    试试下面的 -

             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+" ");
             }
    

    【讨论】:

      猜你喜欢
      • 2015-01-13
      • 2013-11-11
      • 1970-01-01
      • 2013-07-09
      • 2021-11-04
      • 1970-01-01
      • 2015-04-30
      • 2013-07-11
      • 1970-01-01
      相关资源
      最近更新 更多