通过 JSON 发送 64 位值的可接受方式是什么? [英] What is the accepted way to send 64-bit values over JSON?

查看:28
本文介绍了通过 JSON 发送 64 位值的可接受方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一些数据是 64 位整数.我想将这些发送到在页面上运行的 JavaScript 程序.

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

然而,据我所知,大多数 JavaScript 实现中的整数都是 32 位有符号量.

However, as far as I can tell, integers in most JavaScript implementations are 32-bit signed quantities.

我的两个选择似乎是:

  1. 将值作为字符串发送
  2. 将值作为 64 位浮点数发送

选项 (1) 并不完美,但选项 (2) 似乎远没有那么完美(数据丢失).

Option (1) isn't perfect, but option (2) seems far less perfect (loss of data).

你是如何处理这种情况的?

How have you handled this situation?

推荐答案

这似乎不是 JSON 的问题,而是 Javascript 本身的问题.你打算用这些数字做什么?如果它只是一个您稍后需要传回网站的魔法令牌,那么只需使用包含该值的字符串即可.如果您确实必须对值进行算术运算,则可以为 64 位算术编写自己的 Javascript 例程.

This seems to be less a problem with JSON and more a problem with Javascript itself. What are you planning to do with these numbers? If it's just a magic token that you need to pass back to the website later on, by all means simply use a string containing the value. If you actually have to do arithmetic on the value, you could possibly write your own Javascript routines for 64-bit arithmetic.

在 Javascript(以及 JSON)中表示值的一种方法是将数字拆分为两个 32 位值,例如.

One way that you could represent values in Javascript (and hence JSON) would be by splitting the numbers into two 32-bit values, eg.

  [ 12345678, 12345678 ]

要将 64 位值拆分为两个 32 位值,请执行以下操作:

To split a 64-bit value into two 32-bit values, do something like this:

  output_values[0] = (input_value >> 32) & 0xffffffff;
  output_values[1] = input_value & 0xffffffff;

然后将两个 32 位值重新组合为一个 64 位值:

Then to recombine two 32-bit values to a 64-bit value:

  input_value = ((int64_t) output_values[0]) << 32) | output_values[1];

这篇关于通过 JSON 发送 64 位值的可接受方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆