Ok i want to save an array of THREE.Vector3 efficiently to local storage. Since javascript works using Strings, i want to convert a 32bit float to a string using the most efficient bit ratio. i.e ideally 32 bit float = 4 * 8 Bit which is easy to do in something like C++
the problem seems to be 1st Javascript strings are UTF which includes some padding
http://en.wikipedia.org/wiki/UTF-8
and secondly the code i am currently using 0 get converted '' and then omitted, making the converted byte length un-reliable.
String.fromCharCode(0) == ''
var float2str = function(num)
{
var bytestream = new Array();
var view = new DataView(new ArrayBuffer(4));
view.setFloat32(0,num);
bytestream.push(view.getUint8(0));
bytestream.push(view.getUint8(1));
bytestream.push(view.getUint8(2));
bytestream.push(view.getUint8(3));
return String.fromCharCode(view.getUint8(0),view.getUint8(1),view.getUint8(2),view.getUint8(3))
}
var str2float = function(str)
{
var bytestream = unpack(str)
var view = new DataView(new ArrayBuffer(4));
view.setUint8(0,bytestream[0]);
view.setUint8(1,bytestream[1]);
view.setUint8(2,bytestream[2]);
view.setUint8(3,bytestream[3]);
return view.getFloat32(0);
}
thanx!
The trick is in getting the string value of the unsigned 8-bit integers to be printable. I found that if your 8-bit numbers are too small or too large (outside of the "bread and butter" range of ASCII values) you will end up with something that is unprintable. So instead of creating a string of length four using the four ASCII values of the bytes from the float, we can use a string of length 8 using 4-bit values from the float and offsetting those values into the printable range (+32). Here's my solution:
I hope this helps.