Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to convert binary digits to a byte array in TypeScript

1 Answer

0 votes
function binaryToByteArray(binaryString: string): number[] {
    if (binaryString.length % 8 !== 0) {
        throw new Error("Binary string length must be a multiple of 8.");
    }

    const byteList: number[] = [];
    for (let i: number = 0; i < binaryString.length; i += 8) {
        const byteSegment: string = binaryString.slice(i, i + 8);
        const byteValue: number = parseInt(byteSegment, 2);
        byteList.push(byteValue);
    }

    return byteList;
}

const binaryString = "10101110111010101110101001001011";

try {
    const byteList = binaryToByteArray(binaryString);
    console.log("Byte List:", ...byteList);
} catch (e: any) {
    console.error("Error:", e.message);
}
  
  
 
/*
run:
 
"Byte List:",  174,  234,  234,  75 
 
*/
 

 



answered Aug 4, 2025 by avibootz
...