What is the accepted way to send 64-bit values over JSON?

后端 未结 6 636
死守一世寂寞
死守一世寂寞 2020-12-08 20:05

Some of my data are 64-bit integers. I would like to send these to a JavaScript program running on a page.

However, as far as I can tell, integers in most JavaScript

6条回答
  •  囚心锁ツ
    2020-12-08 20:48

    Javascript's Number type (64 bit IEEE 754) only has about 53 bits of precision.

    But, if you don't need to do any addition or multiplication, then you could keep 64-bit value as 4-character strings as JavaScript uses UTF-16.

    For example, 1 could be encoded as "\u0000\u0000\u0000\u0001". This has the advantage that value comparison (==, >, <) works on strings as expected. It also seems straightforward to write bit operations:

    function and64(a,b) {
        var r = "";
        for (var i = 0; i < 4; i++)
            r += String.fromCharCode(a.charCodeAt(i) & b.charCodeAt(i));
        return r;
    }
    

提交回复
热议问题