I am looking to convert a JavaScript number into the smallest possible uint8array representation.
For example :
65 535 = Uint8Array<[255,255]> (0b1111111111111111 = [0b11111111, 0b11111111])
12 356 = Uint8Array<[48,68]> (0b0011000001000100 = [0b00110000, 0b01000100])
1 142 = Uint8Array<[4,118]> (0b0011000001000100 = [0b00000100, 0b01110110])
400 = Uint8Array<[1,144]> (0b0000000110010000 = [0b00000001, 0b10010000])
256 = Uint8Array<[1,0]> (0b0000000100000000 = [0b00000001, 0b00000000])
64 = Uint8Array<[64]> (0b0000000001000000 = [0b01000000])
-1 = Uint8Array<[]> I do not handle negative numbers
I have been attempting to edit bytes individually as shown below:
const buffer : Uint8Array = new Uint8Array([0,0])
buffer[bytePos] &= ~(1<<bitToEdit); //to set at 0
buffer[bytePos] |= (1 <<bitToEdit) //to set at 1
The byte editing operation works, but I am unsure how to extract bytes from the number and populate them into this array.