0

Why am I getting garbage out of the following function?

The incoming _data argument is byte[] = {6D, F3, B4, 15}
The _endian argument is ByteOrder.ByteOrder.LITTLE_ENDIAN

 ByteBuffer m_ByteBuffer; 
 public static double ByteToDouble(byte[] _data, int offset, ByteOrder _endian){
        synchronized (m_ByteBuffer) {
            m_ByteBuffer.clear();
            m_ByteBuffer.order(_endian);
            m_ByteBuffer.position(0);
            m_ByteBuffer.get(_data);
            m_ByteBuffer.position(0);
        }
        return m_ByteBuffer.getDouble();

    }
3
  • Welcome to stackoverflow.com. Please note that this is an English-speaking site, few here understand what looks like Korean. Commented Feb 13, 2020 at 7:11
  • I'm voting to close this question as off-topic because it's not written in english Commented Feb 13, 2020 at 7:12
  • There's no good reason to leave your commented lines in there to confuse and clutter the code. Your job to help us to help you is to make your question as focused and clear and clean and easy for us to figure out what the problem is as possible without having to guess whether you're leaving code in there to explain something to us or just because of laziness. Commented Feb 13, 2020 at 12:57

3 Answers 3

1

I made a little example to see, how the converting could be done back and forth.

Hope it helps.

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class ByteToDouble {

    public static void main(String[] args) throws IOException {
        double d1 = 123.456;
        double d2 = bytesToDouble(doubleToBytes(d1));

        System.out.printf("d1: %f\nd2: %f\n", d1, d2);
    }

    public static byte[] doubleToBytes(double d) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeDouble(d);
        dos.flush();
        return bos.toByteArray();
    }

    public static double bytesToDouble(byte[] bytes) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN);
        return buffer.getDouble();
    }
}

Note that the ByteBuffer#getDouble() method will throw an BufferUnderflowException, if there are fewer than eight bytes remaining in the buffer. (see JavaDoc)

Sign up to request clarification or add additional context in comments.

Comments

0

In your code you use

m_ByteBuffer.get(_data);

which would read the data of the buffer into _data. I suppose what you wanted to use was ByteBuffer#put

m_ByteBuffer.put(_data);

Additionally, after putting data into the buffer, it is more common to use flip instead of position(0) since that correctly sets the limit of the buffer. All in all this would result roughly in:

m_ByteBuffer.order(_endian);

m_ByteBuffer.clear();
m_ByteBuffer.put(_data);
m_ByteBuffer.flip();
m_ByteBuffer.getDouble();

2 Comments

Thank you!! I tried running flip (). However, an error occurred and the program died. Error : E/AndroidRuntime: FATAL EXCEPTION: java.nio.BufferUnderflowException
@RAYZIE to clarify: the BufferUnderflowException gets thrown on the call to getDouble (according to the docs, flip can not throw)? Keep in mind that double means 8 bytes. You might mean to use getFloat.
0

4 bytes represent a float, 8 bytes a double.

byte[] data = {0x6D, 0xF3, 0xB4, 0x15};
double x = bytesToDouble(data);

public static double bytesToDouble(byte[] data) {
    ByteBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);
    return data.length == 4 ? bb.getFloat() : bb.getDouble();
}

ByteBuffer.wrap(byte[]) is a light weight wrapping of the array, with writing capability to the bytes. A global ByteBuffer is not necessarily faster, certainly not when needing synchronisation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.