Javascript 如何在javascript中交换变量的字节序(字节顺序)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5320439/
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
How do I swap endian-ness (byte order) of a variable in javascript
提问by griotspeak
I am receiving and sending a decimal representation of two little endian numbers. I would like to:
我正在接收和发送两个小端数字的十进制表示。我想要:
- shift one variable 8 bits left
- OR them
- shift a variable number of bits
- create 2 8 bit numbers representing the first and second half of the 16 bit number.
- 左移一位变量 8 位
- 或他们
- 移位可变位数
- 创建 2 个 8 位数字,分别代表 16 位数字的前半部分和后半部分。
javascript (according to https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators) uses big endian representation when shifting...
javascript(根据https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators)在移动时使用大端表示...
endianness is a bit foreign to me (I am only 90 percent sure that my outlined steps are what i want.) so swapping is a bit dizzying. please help! I only really need to know how to swap the order in an efficient manner. (I can only think of using a for loop on a toString() return value)
字节序对我来说有点陌生(我只有 90% 的确定我概述的步骤是我想要的。)所以交换有点令人眼花缭乱。请帮忙!我只需要知道如何以有效的方式交换订单。(我只能想到在 toString() 返回值上使用 for 循环)
回答by LukeH
function swap16(val) {
return ((val & 0xFF) << 8)
| ((val >> 8) & 0xFF);
}
Explanation:
解释:
- Let's say that
val
is, for example,0xAABB
. - Mask
val
to get the LSB by&
ingwith0xFF
: result is0xBB
. - Shiftthat result 8 bits to the left: result is
0xBB00
. - Shift
val
8 bits to the right: result is0xAA
(the LSB has "dropped off" the right-hand side). - Mask that result to get the LSB by
&
ingwith0xFF
: result is0xAA
. - Combine the results from steps 3 and step 5 by
|
ingthem together:0xBB00 | 0xAA
is0xBBAA
.
- 比方说,
val
例如,0xAABB
。 - 面膜
val
获得LSB由&
荷兰国际集团与0xFF
:结果0xBB
。 - 将该结果左移8 位:结果为
0xBB00
。 - 移位
val
8位向右:结果是0xAA
(LSB的具有“脱落”的右手侧)。 - 通过
&
ingwith来屏蔽该结果以获取 LSB0xFF
: result is0xAA
。 - 从步骤3结合的结果,并通过步骤5
|
荷兰国际集团在一起:0xBB00 | 0xAA
是0xBBAA
。
function swap32(val) {
return ((val & 0xFF) << 24)
| ((val & 0xFF00) << 8)
| ((val >> 8) & 0xFF00)
| ((val >> 24) & 0xFF);
}
Explanation:
解释:
- Let's say that
val
is, for example,0xAABBCCDD
. - Mask
val
to get the LSB by&
ingwith0xFF
: result is0xDD
. - Shiftthat result 24 bits to the left: result is
0xDD000000
. - Mask
val
to get the second byte by&
ingwith0xFF00
: result is0xCC00
. - Shiftthat result 8 bits to the left: result is
0xCC0000
. - Shift
val
8 bits to the right: result is0xAABBCC
(the LSB has "dropped off" the right-hand side). - Mask that result to get the second byte by
&
ingwith0xFF00
: result is0xBB00
. - Shift
val
24 bits to the right: result is0xAA
(everything except the MSB has "dropped off" the right-hand side). - Mask that result to get the LSB by
&
ingwith0xFF
: result is0xAA
. - Combine the results from steps 3, 5, 7 and 9 by
|
ingthem together:0xDD000000 | 0xCC0000 | 0xBB00 | 0xAA
is0xDDCCBBAA
.
- 比方说,
val
例如,0xAABBCCDD
。 - 面膜
val
获得LSB由&
荷兰国际集团与0xFF
:结果0xDD
。 - 将该结果向左移动24 位:结果是
0xDD000000
。 val
通过&
ingwith获取第二个字节的掩码0xFF00
:结果是0xCC00
。- 将该结果左移8 位:结果为
0xCC0000
。 - 移位
val
8位向右:结果是0xAABBCC
(LSB的具有“脱落”的右手侧)。 - 通过
&
ingwith来屏蔽结果以获取第二个字节0xFF00
:result is0xBB00
。 - 移
val
24位到右:结果是0xAA
(一切,除了最高位具有“脱落”的右侧)。 - 通过
&
ingwith来屏蔽该结果以获取 LSB0xFF
: result is0xAA
。 - 组合来自步骤3,5,7和9通过将结果
|
荷兰国际集团在一起:0xDD000000 | 0xCC0000 | 0xBB00 | 0xAA
是0xDDCCBBAA
。
回答by Mihey Mik
Such function can be used to change endianness in js:
此类函数可用于更改 js 中的字节序:
const changeEndianness = (string) => {
const result = [];
let len = string.length - 2;
while (len >= 0) {
result.push(string.substr(len, 2));
len -= 2;
}
return result.join('');
}
changeEndianness('AA00FF1234'); /// '3412FF00AA'
回答by Martin
Use the << (bit shift) operator. Ex: 1 << 2 == 4.
使用 <<(位移)运算符。例如:1 << 2 == 4。
I really think that the underlying implementation of JavaScript will use whatever endianess the platform it is running on is using. Since you cannot directly access memory in JavaScript you won't ever have to worry about how numbers are represented physically in memory. Bit shifting integer values always yield the same result no matter the endianess. You only see a difference when looking at individual bytes in memory using pointers.
我真的认为 JavaScript 的底层实现将使用它运行的平台正在使用的任何字节序。由于您无法在 JavaScript 中直接访问内存,因此您永远不必担心数字在内存中的物理表示方式。无论字节序如何,位移整数值总是产生相同的结果。使用指针查看内存中的单个字节时,您只会看到不同之处。