JavaScript C 样式类型从有符号转换为无符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14890994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript C Style Type Cast From Signed To Unsigned
提问by Matthias
how to type cast a number in javascript?
如何在javascript中输入数字?
a = (unsigned int)atoi(arg1);
b = (unsigned int)atoi(arg2);
Assuming that a and b can be signed.
假设 a 和 b 可以签名。
I want to convert a 4 byte signed integer into a 4 byte unsigned integer.
我想将 4 字节有符号整数转换为 4 字节无符号整数。
I know there is no such thing as type casting or signed/unsigned in javascript. I am looking for an easy to understand algorithm.
我知道在 javascript 中没有类型转换或签名/未签名之类的东西。我正在寻找一种易于理解的算法。
回答by Vatev
You can try a = arg1>>>0
, but I'm not sure it will do what you are looking for.
你可以试试a = arg1>>>0
,但我不确定它会做你想要的。
See this questionfor more details.
有关更多详细信息,请参阅此问题。
回答by joth
you can also use
你也可以使用
(new Uint32Array([arg1]))[0]
e.g.
例如
< (new Uint32Array([-1]))[0]
> 4294967295
Explanation: JavaScript does not follow traditional machine architecture integer casting conventions like C does, preferring type simplicity and portability over low level efficiency. However the typed-arrays (Uint8Array et al) in JavaScript were added specifically for the purpose of efficient and well defined multi byte and bit-level operations. Thus we can exploit this fact to access well defined and built-in bit casting operations. The syntax in the example above:
说明:JavaScript 不像 C 那样遵循传统的机器架构整数转换约定,它更喜欢类型简单性和可移植性而不是低级效率。然而,JavaScript 中的类型化数组(Uint8Array 等)是专门为了高效且定义良好的多字节和位级操作而添加的。因此,我们可以利用这一事实来访问定义明确的内置位转换操作。上例中的语法:
- Creates a natural-number array of the input
- Constructs a typed array (Uint32Array) from that number. This is where the cast will occur.
- Extracts the first (0-th) element of that typed array, which contains the cast result.
- 创建输入的自然数数组
- 根据该数字构造一个类型化数组 (Uint32Array)。这是演员阵容将发生的地方。
- 提取该类型化数组的第一个(第 0 个)元素,其中包含转换结果。
回答by Alnitak
All (primitive) numbers in Javascript are IEEE748 doubles, giving you 52 bits of integer precision.
Javascript 中的所有(原始)数字都是 IEEE748 双精度数,为您提供 52 位整数精度。
The problem with signed vs unsigned is that all of the Javascript bitwise operators apart from >>>
convert the numbers into a 32-bit signednumber - that is, they take the least significant 32 bits and throw away the rest, and then the resulting bit 31 is sign extended to give a signed result.
有符号与无符号的问题在于,除了>>>
将数字转换为 32 位有符号数字之外的所有 Javascript 按位运算符- 也就是说,它们取最低有效的 32 位并丢弃其余的,然后产生的第 31 位是符号扩展以给出有符号的结果。
If you are starting with the four known byte values, you can get around the problem with the bitwise operators by using simple multiplications and additions instead, which use all 52 bits of integer precision, e.g.
如果您从四个已知字节值开始,您可以通过使用简单的乘法和加法来解决按位运算符的问题,这些乘法和加法使用所有 52 位整数精度,例如
var a = [ 1, 2, 3, 4]; // 0x01020304
var unsigned = a[0] * (1 << 24) + a[1] * (1 << 16) + a[2] * (1 << 8) + a[3]
回答by Shimon Doodkin
convert signed byte to unsigned byte, javascript:
将有符号字节转换为无符号字节,javascript:
-5 & 0xff // = 251 , signed to unsigned byte
251 <<24 >>24 // = -5 , unsinged byte to signed
the first makes all first bits 0 except the 1st byte
第一个使除第一个字节外的所有第一位都为 0
the second found on
第二个发现于
https://blog.vjeux.com/2013/javascript/conversion-from-uint8-to-int8-x-24.html
https://blog.vjeux.com/2013/javascript/conversion-from-uint8-to-int8-x-24.html
basically, it is 4 bytes. in positive number the 3 first bytes are 0 and all 0 bits are zero. in negative number the 3 first bytes are 1 and all 0 bits are one and all 1 bits are zero and the; in bytes the most significant bit is used for sign; shifting bits drags the most significant bit across. so if the sign bit is 1. it makes many 1 bits in front. so it is a side effect of shifting but it works.
基本上,它是4个字节。在正数中,前 3 个字节为 0,所有 0 位均为零。在负数中,前 3 个字节为 1,所有 0 位均为 1,所有 1 位均为零,并且;以字节为单位,最高有效位用于符号;移位位会拖过最重要的位。所以如果符号位是 1. 它在前面有很多 1 位。所以这是转移的副作用,但它有效。