如何在javascript中交换变量的endian-ness(字节顺序) [英] How do I swap endian-ness (byte order) of a variable in javascript

查看:122
本文介绍了如何在javascript中交换变量的endian-ness(字节顺序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在接收并发送两个小端数字的十进制表示。我想:

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);
}

说明:


  1. 假设 val ,例如, 0xAABB

  2. 掩码 val 通过 & ing 0xFF :结果是 0xBB

  3. 转移结果是左边8位:结果是 0xBB00

  4. Shift val 右边8位:结果是 0xAA (LSB已经掉落了右侧)。

  5. 通过 & ing 0xFF :结果是 0xAA

  6. 合并结果来自步骤3和步骤5的 | ing 他们在一起:

    0xBB00 | 0xAA 0xBBAA

  1. Let's say that val is, for example, 0xAABB.
  2. Mask val to get the LSB by &ing with 0xFF: result is 0xBB.
  3. Shift that result 8 bits to the left: result is 0xBB00.
  4. Shift val 8 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 &ing with 0xFF: result is 0xAA.
  6. Combine the results from steps 3 and step 5 by |ing them together:
    0xBB00 | 0xAA is 0xBBAA.







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

说明:


  1. 假设 val ,例如, 0xAABBCCDD

  2. 掩码 val 通过 & ing 0xFF :结果是 0xDD

  3. 转移结果是左边24位:结果是 0xDD000000

  4. 面具 val 通过 & ing <获取第二个字节/ a>与 0xFF00 :结果是 0xCC00

  5. Shift ,向左生成8位:结果为 0xCC0000

  6. Shift val 右边8位:结果是 0xAABBCC (LSB已经掉落右侧)。

  7. 通过 & ing 0xFF00 :结果是 0xBB00

  8. Shift val 右边24位:结果是 0xAA (除了MSB之外的所有内容)已经放弃右侧了。)

  9. 通过 & ing 0xFF :结果是 0xAA

  10. 通过 | ing 他们在一起:

    0xDD000000 | 0xCC0000 | 0xBB00 | 0xAA 0xDDCCBBAA

  1. Let's say that val is, for example, 0xAABBCCDD.
  2. Mask val to get the LSB by &ing with 0xFF: result is 0xDD.
  3. Shift that result 24 bits to the left: result is 0xDD000000.
  4. Mask val to get the second byte by &ing with 0xFF00: result is 0xCC00.
  5. Shift that result 8 bits to the left: result is 0xCC0000.
  6. Shift val 8 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 &ing with 0xFF00: result is 0xBB00.
  8. Shift val 24 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 &ing with 0xFF: result is 0xAA.
  10. Combine the results from steps 3, 5, 7 and 9 by |ing them together:
    0xDD000000 | 0xCC0000 | 0xBB00 | 0xAA is 0xDDCCBBAA.

这篇关于如何在javascript中交换变量的endian-ness(字节顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆