How to Convert a Byte Array into a Double and Back

Java byte array to double conversion

    byte[] bytes = new byte[8];
ByteBuffer.wrap(bytes).putDouble(641.5);
System.out.println("Result: " + Arrays.toString(bytes));
// Result: [64, -124, 12, 0, 0, 0, 0, 0]

So the byte order is little endian (Windows Intel), and especially one byte value went missing, one read() too much; my guess a silly mistake like:

while (read() != -1) {
... read array

The correct data can be converted by:

double val = ByteBuffer.wrap(array).order(ByteOrder.LITTLE_ENDIAN).getDouble();

How can I convert a byte array to a double in python?

Python has the struct module to convert bytes back to float values:

import struct

value = struct.unpack('d', bytes)[0]

Here 'd' signifies that a double value is expected (in native endianess, as 8 bytes). See the module documentation for more options, including specifying endianess.

Another option is to turn your bytes value into an array object; you'd use this is if you had a homogenous sequence of doubles:

import array

doubles_sequence = array.array('d', bytes)

where every 8 bytes is interpreted as a double value, making doubles_sequence a sequence of doubles, addressable by index. To support a different endianess, you can swap the byte order with doubles_sequence.byteswap().

How to convert byte array to double array?

Possible "loopless" solutions would use System.arraycopy or Arrays.copyOf, but in these solutions you cannot avoid casting your byte[] to a double[]. The problem is this casting is impossible according to the specification:

Given a compile-time reference type S (source) and a compile-time reference 
type T (target), a casting conversion exists from S to T if no compile-time
errors occur due to the following rules.
[...]
If S is an array type SC[], that is, an array of components of type SC:
[...]
- If T is an array type TC[], that is, an array of components of type TC,
then a compile-time error occurs unless one of the following is true:
- TC and SC are the same primitive type.
- [...]

Use a loop. See also here.

Java how do I convert a byte[] containing multiple doubles into a double[]

Here it is, approximately. I'm sure I've screwed something up. scaleFactor would presumably be 327.68, to reverse the scaling above. The above code appears to be big endian. Whether you want fullNormalize is up to you.

public byte[] doubleArrayToByteArray(double[] input, int bytesPerSample, double scaleFactor, boolean fullNormalize, boolean bigEndian) {
byte[] result = new byte[input.length * bytesPerSample];
performNormalization(input, scaleFactor, fullNormalize);
for (int i = 0; i < input.length; i++) {
long sourceVal = (long)(input[i] * scaleFactor);
sourceVal = sourceVal >> 8 * (8 - bytesPerSample);
for (int j = 0; j < bytesPerSample; j++) {
int index = i * bytesPerSample;
if (bigEndian) {
index += (bytesPerSample - j);
}
else {
index += j;
}
result[index] = (byte) sourceVal;
sourceVal = sourceVal >> 8;
}
}
return result;
}

public void performNormalization(double[] input, double scaleFactor, boolean fullNormalize) {
double maxVal = 0.0;
for (int i = 0; i < input.length; i++) {
double val = Math.abs(input[i]) * scaleFactor;
if (val > maxVal) {
maxVal = val;
}
}
if (fullNormalize || maxVal > Long.MAX_VALUE) {
double normalizeFactor = (double)(Long.MAX_VALUE) / maxVal;
for (int i = 0; i < input.length; i++) {
input[i] *= normalizeFactor;
}
}
}

Updated: Realized that I needed to account for scaleFactor in normalization. And you'd not normally specify both a scaleFactor that was not 1.0 and fullNormalize = true.

How to convert a byte array into double in C?

Try this:

double a;
memcpy(&a, ptr, sizeof(double));

where ptr is the pointer to your byte array. If you want to avoid copying use a union, e.g.

union {
double d;
char bytes[sizeof(double)];
} u;
// Store your data in u.bytes
// Use floating point number from u.d

converting byte array to double - c

Edit: this won't work (see comment) without something like __int128.

a = input[i] << 8*i;

The expression input[i] is promoted to int (6.3.1.1) , which is 32bit on your machine. To overcome this issue, the lefthand operand has to be 64bit, like in

a = (1L * input[i]) << 8*i;

or

a = (long long unsigned) input[i] << 8*i;

and remember about endianness



Related Topics



Leave a reply



Submit