Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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.

Example:

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

Please let me know, where I am lacking.

like image 200
user1647017 Avatar asked Apr 02 '13 10:04

user1647017


People also ask

What is a 32 bit integer JavaScript?

32-bit integer has range from -2147483648 ( -2^31 ) to 2147483647 ( 2^31 − 1 )

What is ArrayBuffer in JavaScript?

The ArrayBuffer object is used to represent a generic, fixed-length raw binary data buffer. It is an array of bytes, often referred to in other languages as a "byte array".

How do you convert numbers to bytes?

An int object can be used to represent the same value in the format of the byte. The integer represents a byte, is stored as an array with its most significant digit (MSB) stored at either the start or end of the array. An int value can be converted into bytes by using the method int. to_bytes().

Can we convert int to byte?

The byteValue() method of Integer class of java. lang package converts the given Integer into a byte after a narrowing primitive conversion and returns it (value of integer object as a byte).


2 Answers

Now you can use ArrayBuffer and DataView. 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.

like image 136
SEIAROTg Avatar answered Oct 12 '22 11:10

SEIAROTg


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).

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

like image 39
Matthew Cornelisse Avatar answered Oct 12 '22 11:10

Matthew Cornelisse