Reputation: 4130
If I have unpacked binary data
1700885369 # translates to 'easy'
How can I get back to a byte array (most preferably without importing anything)? Like Python's struct.Struct(format).pack
:
>>> import struct
>>> s = struct.Struct('>1I') # a big-endian, two-byte, unsigned int
>>> s.pack(1700885369)
b'easy' # bytearray([101, 97, 115, 121])
Upvotes: 1
Views: 594
Reputation: 700322
You can get a byte at a time from the value and put in an array:
var value = 1700885369;
var arr = [];
while (value > 0) {
arr.unshift(value % 256);
value = Math.floor(value / 256);
}
// display value in StackOverflow snippet
document.write(arr);
Upvotes: 1