title: How to encode integer to Uint8Array and back to integer in JavaScript?
body: <p>I need to add compression to my project and I decided to use the LZJB algorithm that is fast and the code is small. Found this library <a href="https://github.com/copy/jslzjb-k" rel="nofollow noreferrer">https://github.com/copy/jslzjb-k</a></p>
  <p>But the API is not very nice because to decompress the file you need input buffer length (because Uint8Array is not dynamic you need to allocate some data). So I want to save the length of the input buffer as the first few bytes of Uint8Array so I can extract that value and create output Uint8Array based on that integer value.</p>
  <p>I want the function that returns Uint8Array from integer to be generic, maybe save the length of the bytes into the first byte so you know how much data you need to extract to read the integer. I guess I need to extract those bytes and use some bit shifting to get the original number. But I'm not exactly sure how to do this.</p>
  <p>So how can I write a generic function that converts an integer into Uint8Array that can be embedded into a bigger array and then extract that number?</p>