简体   繁体   中英

Converting a []byte array (java double encoded) to Float64

So, I'm trying to decode a byte array to Float64. I've tried a bunch of different ways, found all over StackOverflow, but no luck so far! Here's the go playground link to what I have tried . The expected value should be 3177408.5 . The original value is a Java double, encoded as IEEE 754 floating-point

Edit:

The value is encoded using the org.apache.hadoop.hbase.util.Bytes.toBytes method.

double v = 3445713.95;
long ff;

ff = Double.doubleToRawLongBits(v);

bArr = toBytes(ff)

public static byte[] toBytes(long val) {
    byte[] b = new byte[8];

    for(int i = 7; i > 0; --i) {
        b[i] = (byte)((int)val);
        val >>>= 8;
    }

    b[0] = (byte)((int)val);
    return b;
}

And actually should be: 3445713.95

Edit2:

My bad for actually just pasting code I was given, without thinking much first, Java is not exactly my cup of tea. Turns out there is a problem somewhere along the pipeline (multiple systems communicating), and value is getting corrupted. I'll mark @Ainar-G's solution as correct, since it actually gives back something close to the expected.

The closest I can get is if I use binary.BigEndian.Uint64 :

b := []byte("AJ\x02\xef\xe1\xaf(l")
u := binary.BigEndian.Uint64(b)
f := math.Float64frombits(u)
fmt.Printf("%f", f)

This gives me 3409375.763158 . Make sure you're encoding your float correctly on the Java side.

Playground: https://play.golang.org/p/4KGZoWjLTF .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM