javascript - How to understand endianness in these function: 'writeFloatLE', 'writeFloatBE', 'writeDoubleLE', 'writeDoubleBE' -
i know endianness, such 0xdead
(using writeintle
) can transform ad de
.
however, in these demos:
var buf = new buffer(4); buf.writefloatbe(0xcafebabe, 0); console.log(buf); buf.writefloatle(0xcafebabe, 0); console.log(buf); // <buffer 4f 4a fe bb> // <buffer bb fe 4a 4f>
and
var buf = new buffer(8); buf.writedoublebe(0xdeadbeefcafebabe, 0); console.log(buf); buf.writedoublele(0xdeadbeefcafebabe, 0); console.log(buf); // <buffer 43 eb d5 b7 dd f9 5f d7> // <buffer d7 5f f9 dd b7 d5 eb 43>
i don't know how transform 0xcafebabe
4f 4a fe bb
or bb fe 4a 4f
. me understanding steps, pls.
this has nothing (or not much) endianness. you're passing number written hex litteral writedouble
or writefloat
, turn 32 or 64-bit floating-point representation. is, transformation observe unsigned integer floating-point.
floating-point means number represented m×10e m , e signed integers (well, technically sign stored in separate sign bit).
in case of 64-bit float, 53 bits used digits m , rest (11) of bits encode exponent e. javascript has single number type, , that's 64-bit floating-point. therefore, precision 53 bits (52 unsigned), means cannot store 64-bit integer @ full precision, way.
when parsing code, javascript interpreter convert number literals (such hex literals) internal 64-bit float representation. that number passed writewhatever
@ runtime, not original integer. expect if it's writedouble
there won't conversion (just byte reordering maybe).
Comments
Post a Comment