Convert float [] to byte [] to float [] again

So, here what I am trying to do is get a float[] , convert it to byte[] , send it over the network as a datagram packet and then convert it back to byte[] on the receiving terminal.

Now I know that I can convert float[] to byte[] using the getBytes[] method. But I do not know how to cancel the conversion.

+8
java arrays floating-point byte
source share
6 answers

I think you want to use the ByteBuffer class, which has putFloat and getFloat .

+9
source share

Another way ... use ByteArrayOutputStream / DataOutputStream to output

 float fArr[] = ...; ByteArrayOutputStream bas = new ByteArrayOutputStream(); DataOutputStream ds = new DataOutputStream(bas); for (float f : fArr) ds.writeFloat(f); byte[] bytes = bas.toByteArray(); 

Use ByteArrayInputStream / DataInputStream for input

 byte[] buffer = ...; ByteArrayInputStream bas = new ByteArrayInputStream(buffer); DataInputStream ds = new DataInputStream(bas); float[] fArr = new float[buffer.length / 4]; // 4 bytes per float for (int i = 0; i < fArr.length; i++) { fArr[i] = ds.readFloat(); } 
+5
source share

Use Float.floatToIntBits() to retrieve the float bit value as an integer, then use BigInteger.toByteArray() to create a byte[] . This can be changed using the BigInteger constructor, which takes a byte[] argument, and then Float.intBitsToFloat() .

+2
source share

Sender

 ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); // byteBuffer reused for every element in floatArray ByteBuffer byteBuffer = ByteBuffer.allocate(4); // go through the elements in the float array writing its // byte equivalent to the stream for(float element : floatArray) { byteBuffer.clear(); byteBuffer.putFloat(element) byteStream.write(byteBuffer.array()); } // Logic for sending bytestream bytes as datagram packet // can get byte[] from steam by: byteStream.toByteArray(); 

Receiver

 ArrayList<Float> receivedValues = new ArrayList<Float>(); ByteBuffer byteBuffer = ByteBuffer.wrap(receivedBytes); // while there is still 4 bytes left on the byte buffer // grab the next float and add it to the received list int position = 0; while(byteBuffer.capactiy - position >= 4) { receivedValues.add(byteBuffer.getFloat(position)); position += 4; } float[] result = new float[receivedValues.count]; return receivedValues.toArray(new float[receivedValues.size()]); 
0
source share

You need to use the getFloat () and putFloat () commands in the FloatBuffer ByteBuffer. In fact, you should do this simply because of sheer speed. And this is great to understand for manipulating bytes. You can also mix and match data, and add and receive them as needed. All this is supported by byte buffer. So, the usual thing is to send an array, you also need to send the size of the array.

 public static void writeFloatArray(float[] array, OutputStream stream) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(4 * (array.length + 1)).putInt(array.length); buffer.asFloatBuffer().put(array,0,array.length); stream.write(buffer.array()); } 

You just allocate enough bytes to store everything in the buffer. Write some things, write some other materials, etc. Understanding this point simplifies the job. On the reverse side, we basically do the same thing, although we need additional reading because we don’t know how large the array is, only one:

 public static float[] readFloatArray(InputStream in) throws IOException { byte[] data = new byte[4]; if (readFully(in, data) != data.length) return null; int length = ByteBuffer.wrap(data).getInt(); data = new byte[length * 4]; if (readFully(in,data) != data.length) return null; float[] array = new float[length]; ByteBuffer.wrap(data).asFloatBuffer().get(array,0,array.length); return array; } 

And for full functionality, although not quite from this:

 public static int readFully(InputStream in, byte[] data) throws IOException { int offset = 0; int bytesRead; boolean read = false; while ((bytesRead = in.read(data, offset, data.length - offset)) != -1) { read = true; offset += bytesRead; if (offset >= data.length) { break; } } return (read) ? offset : -1; } 
0
source share
-one
source share

All Articles