javascript 在javascript中将32位整数转换为4字节的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15761790/
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-10-27 02:01:08  来源:igfitidea点击:

Convert a 32bit integer into 4 bytes of data in javascript

javascript

提问by user1647017

I was asked to convert the integers to 32-bit binary numbers. So is used integer.toString(2)and got the required value in 32-bit binary format of 0'sand 1's. But actually what I was asked to do is convert the integer into 4 bytes of data. I am unable to get the output as suggested. I have used integer.toString(8), integer.toString(16). but of no use.

我被要求将整数转换为 32 位二进制数。so 被使用integer.toString(2)并以 0's 和 1's 的 32 位二进制格式得到所需的值。但实际上我被要求做的是将整数转换为 4 个字节的数据。我无法按照建议获得输出。我用过integer.toString(8)integer.toString(16)。但没有用。

Example:

例子:

 num=1065489844 
 num.toString(2) //Output: 111111100000100001010110110100
 num.toString(8) //Output: 7740412664

Please let me know, where I am lacking.

请让我知道,我的不足之处。

回答by SEIAROTg

Now you can use ArrayBufferand DataView. They are native so the performance will be much better if you need to use it very often.

现在您可以使用ArrayBufferDataView。它们是原生的,因此如果您需要经常使用它,性能会好得多。

function toBytesInt32 (num) {
    arr = new ArrayBuffer(4); // an Int32 takes 4 bytes
    view = new DataView(arr);
    view.setUint32(0, num, false); // byteOffset = 0; litteEndian = false
    return arr;
}

equals to

等于

function toBytesInt32 (num) {
    arr = new Uint8Array([
         (num & 0xff000000) >> 24,
         (num & 0x00ff0000) >> 16,
         (num & 0x0000ff00) >> 8,
         (num & 0x000000ff)
    ]);
    return arr.buffer;
}

which use javascript bitwise operators to achieve that.

它使用 javascript 按位运算符来实现这一点。

回答by Matthew Cornelisse

SEIAROTg's answer does not output a string. I have theroughly tested with both positive and negative numbers and this code will give you a 4 byte string output representing the number in big endien format(2's compliment 0xFFFFFFFF = -1).

SEIAROTg 的答案不输出字符串。我已经对正数和负数进行了粗略的测试,这段代码会给你一个 4 字节的字符串输出,代表大端格式的数字(2 的恭维 0xFFFFFFFF = -1)。

var toBytesInt32=function(num) {
    var ascii='';
    for (let i=3;i>=0;i--) {
        ascii+=String.fromCharCode((num>>(8*i))&255);
    }
    return ascii;
};

by the way if you want to go the other way around you can use

顺便说一句,如果你想反过来,你可以使用

var fromBytesInt32=function(numString) {
    var result=0;
    for (let i=3;i>=0;i--) {
        result+=numString.charCodeAt(3-i)<<(8*i);
    }
    return result;
};

to convert a 4 byte string to an integer

将 4 字节字符串转换为整数

回答by Bechir

if you need a hex format output, here is the code.

如果您需要十六进制格式输出,这里是代码。

/* Convert value as 8-bit unsigned integer to 2 digit hexadecimal number. */

function hex8(val) {
    val &= 0xFF;
    var hex = val.toString(16).toUpperCase();
    return ("00" + hex).slice(-2);
}

/* Convert value as 16-bit unsigned integer to 4 digit hexadecimal number. */

function hex16(val) {
    val &= 0xFFFF;
    var hex = val.toString(16).toUpperCase();
    return ("0000" + hex).slice(-4);
}

/* Convert value as 32-bit unsigned integer to 8 digit hexadecimal number. */

function hex32(val) {
    val &= 0xFFFFFFFF;
    var hex = val.toString(16).toUpperCase();
    return ("00000000" + hex).slice(-8);
}


var num = 1065489844;
console.log("0x" + hex32(num)); // will output 0x3F8215B4