What Is the Fastest Way to Convert a Float[] to a Byte[]

What is the fastest way to convert a float[] to a byte[]?

If you do not want any conversion to happen, I would suggest Buffer.BlockCopy().

public static void BlockCopy(
Array src,
int srcOffset,
Array dst,
int dstOffset,
int count
)

For example:

float[] floatArray = new float[1000];
byte[] byteArray = new byte[floatArray.Length * 4];

Buffer.BlockCopy(floatArray, 0, byteArray, 0, byteArray.Length);

What is the Fastest way to convert byte[] to float[] and vice versa?

Surely msarchet's proposal makes copies too. You are talking about just changing the way .NET thinks about a memory area, if you dont' want to copy.

But, I don't think what you want is possible, as bytes and floats are represented totally different in memory. A byte uses exactly a byte in memory, but a float uses 4 bytes (32 bits).

If you don't have the memory requirements to store your data, just represent the data as the data type you will be using the most in memory, and convert the values you actually use, when you use them.

How do you want to convert a float (which can represent a value between ±1.5 × 10−45 and±3.4 × 10^38) into a byte (which can represent a value between 0 and 255) anyway?

(see more info her about:

  • byte: http://msdn.microsoft.com/en-us/library/5bdb6693(v=VS.100).aspx
  • float: http://msdn.microsoft.com/en-us/library/b1e65aza.aspx

More about floating types in .NET here: http://csharpindepth.com/Articles/General/FloatingPoint.aspx

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

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

C Function to Convert float to byte array

Easiest is to make a union:

#include <stdio.h>

int main(void) {
int ii;
union {
float a;
unsigned char bytes[4];
} thing;

thing.a = 1.234;
for (ii=0; ii<4; ii++)
printf ("byte %d is %02x\n", ii, thing.bytes[ii]);
return 0;
}

Output:

byte 0 is b6
byte 1 is f3
byte 2 is 9d
byte 3 is 3f

Note - there is no guarantee about the byte order… it depends on your machine architecture.

To get your function to work, do this:

void float2Bytes(byte bytes_temp[4],float float_variable){ 
union {
float a;
unsigned char bytes[4];
} thing;
thing.a = float_variable;
memcpy(bytes_temp, thing.bytes, 4);
}

Or to really hack it:

void float2Bytes(byte bytes_temp[4],float float_variable){ 
memcpy(bytes_temp, (unsigned char*) (&float_variable), 4);
}

Note - in either case I make sure to copy the data to the location given as the input parameter. This is crucial, as local variables will not exist after you return (although you could declare them static, but let's not teach you bad habits. What if the function gets called again…)

What is the Fastest way to convert byte[] to float[] and vice versa?

Surely msarchet's proposal makes copies too. You are talking about just changing the way .NET thinks about a memory area, if you dont' want to copy.

But, I don't think what you want is possible, as bytes and floats are represented totally different in memory. A byte uses exactly a byte in memory, but a float uses 4 bytes (32 bits).

If you don't have the memory requirements to store your data, just represent the data as the data type you will be using the most in memory, and convert the values you actually use, when you use them.

How do you want to convert a float (which can represent a value between ±1.5 × 10−45 and±3.4 × 10^38) into a byte (which can represent a value between 0 and 255) anyway?

(see more info her about:

  • byte: http://msdn.microsoft.com/en-us/library/5bdb6693(v=VS.100).aspx
  • float: http://msdn.microsoft.com/en-us/library/b1e65aza.aspx

More about floating types in .NET here: http://csharpindepth.com/Articles/General/FloatingPoint.aspx

Converting a float to bytearray

It depends what you want, and what you are going to do with it. If all you want is a bytearray then:

import struct

value = 5.1

ba = bytearray(struct.pack("f", value))

Where ba is a bytearray. However, if you wish to display the hex values (which I suspect), then:

print([ "0x%02x" % b for b in ba ])

EDIT:

This gives (for value 5.1):

['0x33', '0x33', '0xa3', '0x40']

However, CPython uses the C type double to store even small floats (there are good reasons for that), so:

value = 5.1
ba = bytearray(struct.pack("d", value))
print([ "0x%02x" % b for b in ba ])

Gives:

['0x66', '0x66', '0x66', '0x66', '0x66', '0x66', '0x14', '0x40']

How to convert a float into a byte array and vice versa?

Use these instead.

public static byte [] long2ByteArray (long value)
{
return ByteBuffer.allocate(8).putLong(value).array();
}

public static byte [] float2ByteArray (float value)
{
return ByteBuffer.allocate(4).putFloat(value).array();
}


Related Topics



Leave a reply



Submit