Java 再次将浮点[]转换为字节[]再转换为浮点[]
所以我在这里要做的是得到一个Java 再次将浮点[]转换为字节[]再转换为浮点[],java,arrays,floating-point,byte,Java,Arrays,Floating Point,Byte,所以我在这里要做的是得到一个float[],将它转换成byte[],作为数据报包通过网络发送,然后在接收终端将它转换回byte[] 现在我知道我可以使用getBytes[]方法将float[]转换为byte[]。但是我不知道如何反转转换。Byte类有一个floatValue()方法 我想你应该使用ByteBuffer类,它有putFloat和getFloat方法。使用Float.floatToIntBits()提取浮点的位值作为整数,然后使用BigInteger.toByteArray()生成一
float[]
,将它转换成byte[]
,作为数据报包通过网络发送,然后在接收终端将它转换回byte[]
现在我知道我可以使用
getBytes[]
方法将float[]
转换为byte[]
。但是我不知道如何反转转换。Byte类有一个floatValue()方法
我想你应该使用
ByteBuffer
类,它有putFloat
和getFloat
方法。使用Float.floatToIntBits()
提取浮点的位值作为整数,然后使用BigInteger.toByteArray()
生成一个字节[]
。这可以使用biginger
构造函数反转,该构造函数接受byte[]
参数,然后以另一种方式使用Float.intBitsToFloat()。。。使用ByteArrayOutputStream/DataOutputStream进行输出
float fArr[] = ...;
ByteArrayOutputStream bas = new ByteArrayOutputStream();
DataOutputStream ds = new DataOutputStream(bas);
for (float f : fArr)
ds.writeFloat(f);
byte[] bytes = bas.toByteArray();
使用ByteArrayInputStream/DataInputStream进行输入
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();
}
byte[]缓冲区=。。。;
ByteArrayInputStream bas=新的ByteArrayInputStream(缓冲区);
DataInputStream ds=新的DataInputStream(bas);
float[]fArr=新的float[buffer.length/4];//每个浮点4字节
对于(int i=0;i
发送方:
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's bytes as datagram packet
// can get byte[] from steam by: byteStream.toByteArray();
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()]);
接收器:
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's bytes as datagram packet
// can get byte[] from steam by: byteStream.toByteArray();
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()]);
ArrayList receivedValues=new ArrayList();
ByteBuffer ByteBuffer=ByteBuffer.wrap(接收字节);
//而字节缓冲区上还有4个字节
//抓取下一个浮点数并将其添加到接收列表中
int位置=0;
while(byteBuffer.capactiy-位置>=4){
receivedValues.add(byteBuffer.getFloat(position));
位置+=4;
}
float[]结果=新的float[receivedValues.count];
返回receivedValues.toArray(新浮点[receivedValues.size()]);
您需要在ByteBuffer的FloatBuffer中使用getFloat()和putFloat()命令。事实上,你当然应该这么做,因为你的速度太快了。对于字节操作来说,理解它是一件很棒的事情。您还可以混合和匹配数据,并根据需要放置和获取数据。所有这些都由字节缓冲区支持。因此,发送数组的常见情况是,还需要发送数组的大小
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());
}
您只需确保分配足够的字节来存储缓冲区中的所有内容。写一些东西,写一些其他的东西等等。理解这一点会让事情变得更容易。另一方面,我们做的基本相同,尽管我们需要额外的读取,因为我们不知道数组有多大,只是有一个:
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;
}
对于完整的功能,尽管不完全是其中的一部分:
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;
}
这是我以后的参考资料
public static byte[] floatToByte(float[] input) {
byte[] ret = new byte[input.length*4];
for (int x = 0; x < input.length; x++) {
ByteBuffer.wrap(ret, x*4, 4).putFloat(input[x]);
}
return ret;
}
public static float[] byteToFloat(byte[] input) {
float[] ret = new float[input.length/4];
for (int x = 0; x < input.length; x+=4) {
ret[x/4] = ByteBuffer.wrap(input, x, 4).getFloat();
}
return ret;
}
公共静态字节[]浮点字节(浮点[]输入){
字节[]ret=新字节[input.length*4];
对于(int x=0;x
可以简化为一行,如。ByteBuffer文档:
final static int nbBytesInFloat=Float.SIZE/Byte.SIZE;
公共静态字节[]toByteArray(浮点[]浮点数组){
字节[]结果=新字节[floatArray.length*nbBytesInFloat];
ByteBuffer wrappedBytes=ByteBuffer.wrap(结果);
对于(int i=0;i这不会给你一般的浮点值,只会给那些代表0到255之间的整数的值。一个浮点值需要4个字节,Byte类的floatValue方法只能使用一个字节。谢谢!我想就是这样了。等我有时间的时候我会试试,我会让你知道会发生什么。@brain56:如果你是在网络上结束此操作时,您可能希望显式指定编码,而不是任由平台的默认编码支配。ByteBuffer还具有asFloatBuffer()方法,如果您不想单独提取值,则可以链接方法调用:ByteBuffer.wrap(someByteArray).asFloatBuffer().array()要将字节[]转换为浮点[]@StevenMagana Zook,您可以将其作为答案发布。没有writeFloat这样的方法-您的代码根本无法编译并指出它,我已使用正确的适配器类更新了代码。这里回答得很好: