Tag: bytearray

将LongBuffer / IntBuffer / ShortBuffer转换为ByteBuffer

我知道一种快速的方法将byte / short / int / long数组转换为ByteBuffer,然后获取一个字节数组。 例如,要将字节数组转换为短数组,我可以这样做: byte[] bArray = { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }; ByteBuffer bb = ByteBuffer.wrap(byteArray); ShortBuffer sb = bb.asShortBuffer(); short[] shortArray = new short[byteArray.length / 2]; sb.get(shortArray); 产生如下的短数组: [256, 0, 0, 0, 256, 0, 0, 0] 。 […]

这个Java ByteBuffer的行为有解释吗?

我需要将数值转换为字节数组。 例如,要将long转换为byte数组,我有以下方法: public static byte[] longToBytes(long l) { ByteBuffer buff = ByteBuffer.allocate(8); buff.order(ByteOrder.BIG_ENDIAN); buff.putLong(l); return buff.array(); } 这很简单 – 花一点时间,分配一个可以容纳它的数组,并把它放在那里。 无论l的值是什么,我都会得到一个8字节的数组,然后我可以按照预期进行处理和使用。 就我而言,我正在创建一个自定义二进制格式,然后通过网络传输它。 当我使用值773450364调用此方法时,我得到一个数组[0 0 0 0 46 25 -22 124] 。 我有代码也将字节数组转换回它们的数值: public static Long bytesToLong(byte[] aBytes, int start) { byte[] b = new byte[8]; b[0] = aBytes[start + 0]; b[1] = aBytes[start + 1]; […]

快速ByteBuffer到CharBuffer或char

将java.nio.ByteBuffer a转换为(新创建的) CharBuffer b或char[] b的最快方法是什么? 通过这样做很重要, a[i] == b[i] 。 这意味着,不是a[i]和a[i+1]一起组成一个值b[j] , getChar(i)会做什么,但这些值应该是“传播”。 byte a[] = { 1,2,3, 125,126,127, -128,-127,-126 } // each a byte (which are signed) char b[] = { 1,2,3, 125,126,127, 128, 129, 130 } // each a char (which are unsigned) 请注意, byte:-128与char:128具有相同(低8位)的位。 因此,我假设“最佳”解释将如上所述,因为位是相同的。 之后我还需要相反的翻译:将char[]或java.nio.CharBuffer重新放回java.nio.ByteBuffer的最有效方法。

创建文件时为8192个字节

在我的Java代码中,我有一个函数,它在http请求中从客户端获取文件并将其转换为文件。 我有这条线: byte[] buffer = new byte[8192]; 8192字节(8 kb)在这里意味着什么? 这是我得到的回复之一 ,并希望确保我理解该代码。

如何在Java中将字节数组转换为hex格式

我知道您可以使用printf并使用StringBuilder.append(String.format(“%x”, byte))将值转换为HEX值并在控制台上显示它们。 但我希望能够实际格式化字节数组,以便每个字节显示为HEX而不是十进制。 这是我的代码的一部分,我已经说过了我说的前两种方式: if(bytes > 0) { byteArray = new byte[bytes]; // Set up array to receive these values. for(int i=0; i 0) { byteArray[i] = (byte)Integer.parseInt(byteString, 16); // Parse value into binary data array. } else { System.out.println(“String is empty!”); } offSet += CHARSPERBYTE; // Set up for next word hex. } StringBuilder […]

如何将Java字节数组转换为Scala字节数组?

我是Scala的新手,目前正在参与涉及Java和Scala模块的项目。 现在我想使用byte []类型的参数从Java调用Scala方法。 Scala方法有签名: def foo(data: Array[Byte]) Java调用如下所示: foo(x) ,其中x的类型为byte[] 。 IDE告诉我它不可能: The method foo(Array) in the type Bar is not applicable for the arguments (byte[]) 作为附加约束,不优选更改Scala方法。 在Java方面,我尝试使用Byte[] ,但这并没有解决问题。 必须存在一些转换?

音频:更改字节数组中的样本量

我正在使用这种方法将wav文件读取到字节数组(如下所示) 。 现在我把它存储在我的字节数组中,我想改变音量。 private byte[] getAudioFileData(final String filePath) { byte[] data = null; try { final ByteArrayOutputStream baout = new ByteArrayOutputStream(); final File file = new File(filePath); final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file); byte[] buffer = new byte[4096]; int c; while ((c = audioInputStream.read(buffer, 0, buffer.length)) != -1) { baout.write(buffer, 0, c); } audioInputStream.close(); baout.close(); data […]

Java中字节数组的可用内存

释放由字节数组分配的内存的最佳方法是什么(Java中的新字节[size])?

适用于Java的Bitconverter

按照问题https://stackoverflow.com/questions/1738244/what-is-the-java-equivalent-of-net-bitconverter中提供的建议,我已经开始为Java实现我自己的bitconverter但是没有得到相同的结果。 有人可以指导我可能做错了吗? public static byte[] GetBytes(Integer value) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream stream = new DataOutputStream(byteStream); try { stream.writeInt(value); } catch (IOException e) { return new byte[4]; } return byteStream.toByteArray(); } byte[] result = BitConverter.GetBytes(1234); //JAVA: [0, 0, 4, -46] byte[] result = BitConverter.GetBytes(1234); //C#: [210, 4, 0, 0]

Java Jersey:接收表单参数作为字节数组

是否可以使用Jersey接收form参数作为字节数组? 我尝试了以下方法: @Path(“/someMethod”) @POST @Produces(MediaType.TEXT_HTML) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String someMethod(@FormParam(“someParam”) byte[] someParam) { return “”; } 但得到了这个错误: SEVERE: The following errors and warnings have been detected with resource and/or provider classes: SEVERE: Missing dependency for method public java.lang.String SomeClass.someMethod(byte[]) at parameter at index 0 SEVERE: Missing dependency for method public java.lang.String SomeClass.someMethod(byte[]) at parameter at index […]