1

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.