The project I'm working on requires me to store javascript numbers(which are doubles) as BLOB primary keys in a database table(can't use the database native float data type). So basically I need to serialize numbers to a byte array in such a way that:
1 - The length of the byte array is 8(which is what is normally needed to serialize doubles)
2 - the byte arrays must preserve natural order so the database will transparently sort rows in the index b-tree.
A simple function that takes a number and returns an array of numbers representing the bytes is what I'm seeking. I prefer the function to be written in javascript but answers in java, C, C#, C++ or python will also be accepted.
The obvious answer is to remove the restriction that you can't use the native database type. I can't see any point in it. It's still 8 bytes and it does the ordering for you without any further investigation, work, experiment, testing etc being necessary.
How to efficiently serialize 64-bit floats so that the byte arrays preserve natural numeric order?