简体   繁体   English

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

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

I was asked to convert the integers to 32-bit binary numbers.我被要求将整数转换为 32 位二进制数。 So is used integer.toString(2) and got the required value in 32-bit binary format of 0'sand 1's.因此使用integer.toString(2)并以 0 和 1 的 32 位二进制格式获得所需的值。 But actually what I was asked to do is convert the integer into 4 bytes of data.但实际上我被要求做的是将整数转换为 4 个字节的数据。 I am unable to get the output as suggested.我无法按照建议获得输出。 I have used integer.toString(8) , integer.toString(16) .我用过integer.toString(8)integer.toString(16) but of no use.但没有用。

Example:例子:

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

Please let me know, where I am lacking.请让我知道,我的不足之处。

Now you can use ArrayBuffer and DataView .现在您可以使用ArrayBufferDataView They are native so the performance will be much better if you need to use it very often.它们是原生的,因此如果您需要经常使用它,性能会好得多。

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 按位运算符来实现这一点。

SEIAROTg's answer does not output a string. SEIAROTg 的答案不输出字符串。 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).我已经对正数和负数进行了粗略的测试,这段代码会给你一个 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 字节字符串转换为整数

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM