我在服务器端 (c#) 有一个整数 a
:
int a = 65512;
当我可以将它转换为 short 时:(short)a
等于 -24
我想将此转换转移到客户端 (javascript)
我尝试先将其转换为二进制:a.toString(2)
然后执行 a.toString(2) & 0xFF
但徒劳无功
如何在 javascript 端将数字转换为短数字?
最佳答案
您可以使用 TypedArray
将 JavaScript 中的数字强制转换为特定的数字类型。的,具体来说,Int16Array
:
function toShort(number) {
const int16 = new Int16Array(1)
int16[0] = number
return int16[0]
}
console.log(toShort(65512))
关于javascript - 如何在javascript中将整数转换为短整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50179214/