简体   繁体   中英

How can I convert from a Javascript Buffer to a String only containing 0 and 1

So I currently am trying to implement the huffman alg and it works fine for decoding and encoding. However, I store the encoded data as follows.

The result of the encoding function is a list containing many strings made up of 0 and 1 and all are varying length.

If i'd safe them in a normal txt file it would take up more space, if Id store them how they are in a binary file it could be that for example an 'e' which would have the code 101 would be stored in a full 8 bits looking like '00000101' which is wasteful and wont take up less storage then the original txt file. I took all the strings in the list and put them into one string and split it into equal parts of length 8 to store them more effectively.

However if I wanna read the data now, instead of 0 and 1 I get utf-8 chars, even some escape characters.

I'm reading the file with fs.readFileSync("./encoded.bin", "binary"); but javascript then thinks it's a buffer already and converts it to a string and it gets all weird... Any solutions or ideas to convert it back to 0 and 1?

I also tried to switch the "binary" in fs.readFileSync("./encoded.bin", "binary"); to a "utf-8" which helped with not crashing my terminal but still is "#��C��Ʃ��Ԧ�y�Kf�g��<�e�t"

To clarify, my goal in the end is to read out the massive string of binary data which would look like this "00011001000101001010" and actually get this into a string...

You can convert a String of 1 s and 0 s to the numerical representation of a byte using Number.parseInt(str, 2) and to convert it back, you can use nr.toString(2) .

The entire process will look something like this:

 const original = '0000010100000111'; // Split the string in 8 char long substrings const stringBytes = original.match(/.{8}/g); // Convert the 8 char long strings to numerical byte representations const numBytes = stringBytes.map((s) => Number.parseInt(s, 2)); // Convert the numbers to an ArrayBuffer const buffer = Uint8Array.from(numBytes); // Write to file // Read from file and reverse the process const decoded = [...buffer].map((b) => b.toString(2).padStart(8, '0')).join(''); console.log('original', original, 'decoded', decoded, 'same', original === decoded);

var binary = fs.readFileSync("./binary.bin");
binary = [...binary].map((b) => b.toString(2).padStart(8, "0")).join("");
console.log(binary);

//Output will be like 010000111011010

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM