如何在javascript中交换变量的endian-ness(字节顺序) [英] How do I swap endian-ness (byte order) of a variable in javascript
问题描述
我正在接收并发送两个小端数字的十进制表示。我想:
I am receiving and sending a decimal representation of two little endian numbers. I would like to:
- 将一个变量向左移8位
- 或者他们
- 移位可变位数
- 创建2个8位数字,代表16位数字的前半部分和后半部分。
- 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.
javascript(根据 https ://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators )在转移时使用大端表示...
javascript (according to https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators) uses big endian representation when shifting...
endianness是一个对我来说有点陌生(我只有90%确定我概述的步骤是我想要的。)所以交换有点令人眼花缭乱。请帮忙!我真的只需要知道如何以有效的方式交换订单。 (我只能想到在toString()返回值上使用for循环)
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)
推荐答案
function swap16(val) {
return ((val & 0xFF) << 8)
| ((val >> 8) & 0xFF);
}
说明:
- 假设
val
,例如,0xAABB
。 - 掩码
val
通过&
ing ,0xFF
:结果是0xBB
。 - 转移结果是左边8位:结果是
0xBB00
。 - Shift
val
右边8位:结果是0xAA
(LSB已经掉落了右侧)。 - 通过
&
ing0xFF
:结果是0xAA
。 - 合并结果来自步骤3和步骤5的
|
ing 他们在一起:
0xBB00 | 0xAA
是0xBBAA
。
- Let's say that
val
is, for example,0xAABB
. - Mask
val
to get the LSB by&
ing with0xFF
: result is0xBB
. - Shift that 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
&
ing with0xFF
: result is0xAA
. - Combine the results from steps 3 and step 5 by
|
ing them together:
0xBB00 | 0xAA
is0xBBAA
.
function swap32(val) {
return ((val & 0xFF) << 24)
| ((val & 0xFF00) << 8)
| ((val >> 8) & 0xFF00)
| ((val >> 24) & 0xFF);
}
说明:
- 假设
val
,例如,0xAABBCCDD
。 - 掩码
val
通过&
ing ,0xFF
:结果是0xDD
。 - 转移结果是左边24位:结果是
0xDD000000
。 - 面具
val
通过&
ing <获取第二个字节/ a>与0xFF00
:结果是0xCC00
。 - Shift ,向左生成8位:结果为
0xCC0000
。 - Shift
val
右边8位:结果是0xAABBCC
(LSB已经掉落右侧)。 - 通过
&
ing ,0xFF00
:结果是0xBB00
。 - Shift
val
右边24位:结果是0xAA
(除了MSB之外的所有内容)已经放弃右侧了。) - 通过
&
ing ,0xFF
:结果是0xAA
。 - 通过
|
ing 他们在一起:
0xDD000000 | 0xCC0000 | 0xBB00 | 0xAA
是0xDDCCBBAA
。
- Let's say that
val
is, for example,0xAABBCCDD
. - Mask
val
to get the LSB by&
ing with0xFF
: result is0xDD
. - Shift that result 24 bits to the left: result is
0xDD000000
. - Mask
val
to get the second byte by&
ing with0xFF00
: result is0xCC00
. - Shift that 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
&
ing with0xFF00
: 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
&
ing with0xFF
: result is0xAA
. - Combine the results from steps 3, 5, 7 and 9 by
|
ing them together:
0xDD000000 | 0xCC0000 | 0xBB00 | 0xAA
is0xDDCCBBAA
.
这篇关于如何在javascript中交换变量的endian-ness(字节顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!