如何将短数组转换为字节数组 [英] how to convert short array to byte array
问题描述
我发现转换短到Byte数组和<一个href=\"http://stackoverflow.com/questions/5625573/byte-array-to-short-array-and-back-again-in-java\">byte阵列短阵,但也不短阵字节数组。
下面是code领导到转换
而(!停止)
{
Log.i(地图,写入新数据缓冲区);
短[] =缓冲缓冲[IX +%buffers.length]。 N = recorder.read(缓冲液,0,buffer.length);
track.write(缓冲液,0,buffer.length); 字节[]字节2 =新的字节[N];
我曾尝试
INT I = 0;
ByteBuffer的byteBuf = ByteBuffer.allocate(N);
而(buffer.length&GT; = 1){
byteBuf.putShort(缓冲[I]);
我++;
}字节2 = byteBuf.array();
和
ByteBuffer.wrap(字节2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer()把(缓冲);
不过,我收到这两个错误(错误,如果不完全相同,但两个非常类似):
13 05-29:41:12.021:W / AudioTrack(9758):obtainBuffer()的轨道0x30efa0
禁用,重新启动
13 05-29:41:12.857:W / AudioWorker(9758):读取错误的声音
AudioWorker
13 05-29:41:12.857:W / AudioWorker(9758):
java.nio.BufferOverflowException
13 05-29:41:12.857:W / AudioWorker(9758):在
java.nio.ShortBuffer.put(ShortBuffer.java:422)
13 05-29:41:12.857:W / AudioWorker(9758):在
java.nio.ShortToByteBufferAdapter.put(ShortToByteBufferAdapter.java:210)
13 05-29:41:12.857:W / AudioWorker(9758):在
java.nio.ShortBuffer.put(ShortBuffer.java:391)
13 05-29:41:12.857:W / AudioWorker(9758):在
com.avispl.nicu.audio.AudioWorker.run(AudioWorker.java:126)
块引用>和刚需给尽可能多的信息尽可能这里是code使用的字节数组后
Log.i(地图,测试);
//转换为ULAW
读(字节2,O,N); //发送到服务器
os.write(bytes2,0,bytes2.length); 的System.out.println(读取动作+ buffer.length);
的System.out.println(数据+ Arrays.toString(缓冲));
}
解决方案Java的
短
是16位的,而字节
是一个8位的类型。您有试图插入N
短裤到缓冲区这是N
循环-bytes长;它需要2 * N
字节长,以适应您的所有数据。的ByteBuffer byteBuf = ByteBuffer.allocate(2 * N);
而(N&GT; = 1){
byteBuf.putShort(缓冲[I]);
我++;
}I have found converting a short to byte array, and byte array to short array, but not short array to byte array.
Here is the code leading up to the conversion
while(!stopped) { Log.i("Map", "Writing new data to buffer"); short[] buffer = buffers[ix++ % buffers.length]; N = recorder.read(buffer,0,buffer.length); track.write(buffer, 0, buffer.length); byte[] bytes2 = new byte[N];
I have tried
int i = 0; ByteBuffer byteBuf = ByteBuffer.allocate(N); while (buffer.length >= i) { byteBuf.putShort(buffer[i]); i++; } bytes2 = byteBuf.array();
and
ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(buffer);
However I receive this error on both (the error if not exactly the same but very similar for both):
05-29 13:41:12.021: W/AudioTrack(9758): obtainBuffer() track 0x30efa0 disabled, restarting
05-29 13:41:12.857: W/AudioWorker(9758): Error reading voice AudioWorker
05-29 13:41:12.857: W/AudioWorker(9758): java.nio.BufferOverflowException
05-29 13:41:12.857: W/AudioWorker(9758): at java.nio.ShortBuffer.put(ShortBuffer.java:422)
05-29 13:41:12.857: W/AudioWorker(9758): at java.nio.ShortToByteBufferAdapter.put(ShortToByteBufferAdapter.java:210)
05-29 13:41:12.857: W/AudioWorker(9758): at java.nio.ShortBuffer.put(ShortBuffer.java:391)
05-29 13:41:12.857: W/AudioWorker(9758): at com.avispl.nicu.audio.AudioWorker.run(AudioWorker.java:126)
And just to be give as much info as possible here is the code after that uses the byte array
Log.i("Map", "test"); //convert to ulaw read(bytes2, 0, N); //send to server os.write(bytes2,0,bytes2.length); System.out.println("bytesRead "+buffer.length); System.out.println("data "+Arrays.toString(buffer)); }
解决方案Java
short
is a 16-bit type, andbyte
is an 8-bit type. You have a loop that tries to insertN
shorts into a buffer that'sN
-bytes long; it needs to be2*N
bytes long to fit all your data.ByteBuffer byteBuf = ByteBuffer.allocate(2*N); while (N >= i) { byteBuf.putShort(buffer[i]); i++; }
这篇关于如何将短数组转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!