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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:39:35  来源:igfitidea点击:

How do I swap endian-ness (byte order) of a variable in javascript

javascriptendianness

提问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:

解释:

  1. Let's say that valis, for example, 0xAABB.
  2. Mask valto get the LSB by &ingwith 0xFF: result is 0xBB.
  3. Shiftthat result 8 bits to the left: result is 0xBB00.
  4. Shiftval8 bits to the right: result is 0xAA(the LSB has "dropped off" the right-hand side).
  5. Mask that result to get the LSB by &ingwith 0xFF: result is 0xAA.
  6. Combine the results from steps 3 and step 5 by |ingthem together:
    0xBB00 | 0xAAis 0xBBAA.
  1. 比方说,val例如,0xAABB
  2. 面膜val获得LSB由&荷兰国际集团0xFF:结果0xBB
  3. 将该结果左移8 位:结果为0xBB00
  4. 移位val8位向右:结果是0xAA(LSB的具有“脱落”的右手侧)。
  5. 通过&ingwith来屏蔽该结果以获取 LSB 0xFF: result is 0xAA
  6. 从步骤3结合的结果,并通过步骤5|荷兰国际集团在一起:
    0xBB00 | 0xAA0xBBAA


function swap32(val) {
    return ((val & 0xFF) << 24)
           | ((val & 0xFF00) << 8)
           | ((val >> 8) & 0xFF00)
           | ((val >> 24) & 0xFF);
}

Explanation:

解释:

  1. Let's say that valis, for example, 0xAABBCCDD.
  2. Mask valto get the LSB by &ingwith 0xFF: result is 0xDD.
  3. Shiftthat result 24 bits to the left: result is 0xDD000000.
  4. Mask valto get the second byte by &ingwith 0xFF00: result is 0xCC00.
  5. Shiftthat result 8 bits to the left: result is 0xCC0000.
  6. Shiftval8 bits to the right: result is 0xAABBCC(the LSB has "dropped off" the right-hand side).
  7. Mask that result to get the second byte by &ingwith 0xFF00: result is 0xBB00.
  8. Shiftval24 bits to the right: result is 0xAA(everything except the MSB has "dropped off" the right-hand side).
  9. Mask that result to get the LSB by &ingwith 0xFF: result is 0xAA.
  10. Combine the results from steps 3, 5, 7 and 9 by |ingthem together:
    0xDD000000 | 0xCC0000 | 0xBB00 | 0xAAis 0xDDCCBBAA.
  1. 比方说,val例如,0xAABBCCDD
  2. 面膜val获得LSB由&荷兰国际集团0xFF:结果0xDD
  3. 将该结果向左移动24 位:结果是0xDD000000
  4. val通过&ingwith获取第二个字节的掩码0xFF00:结果是0xCC00
  5. 将该结果左移8 位:结果为0xCC0000
  6. 移位val8位向右:结果是0xAABBCC(LSB的具有“脱落”的右手侧)。
  7. 通过&ingwith来屏蔽结果以获取第二个字节0xFF00:result is 0xBB00
  8. val24位到右:结果是0xAA(一切,除了最高位具有“脱落”的右侧)。
  9. 通过&ingwith来屏蔽该结果以获取 LSB 0xFF: result is 0xAA
  10. 组合来自步骤3,5,7和9通过将结果|荷兰国际集团在一起:
    0xDD000000 | 0xCC0000 | 0xBB00 | 0xAA0xDDCCBBAA

回答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 中直接访问内存,因此您永远不必担心数字在内存中的物理表示方式。无论字节序如何,位移整数值总是产生相同的结果。使用指针查看内存中的单个字节时,您只会看到不同之处。