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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,239 questions

56,142 answers

573 users

How to convert binary digits to a byte list in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class BinaryConverter
{
    public static List<byte> BinaryToByteList(string binaryString) {
        var byteList = new List<byte>();

        // Ensure the binary string length is a multiple of 8
        if (binaryString.Length % 8 != 0) {
            throw new ArgumentException("Binary string length must be a multiple of 8.");
        }

        // Process each 8-bit chunk
        for (int i = 0; i < binaryString.Length; i += 8) {
            string byteString = binaryString.Substring(i, 8);
            byte byteValue = Convert.ToByte(byteString, 2);
            byteList.Add(byteValue);
        }

        return byteList;
    }

    static void Main()
    {
        string binaryString = "10101110111010101110101001001011";

        try
        {
            List<byte> byteList = BinaryToByteList(binaryString);

            Console.Write("Byte List: ");
            foreach (byte b in byteList) {
                Console.Write($"{b} ");
            }
            Console.WriteLine();
        }
        catch (Exception e)
        {
            Console.Error.WriteLine($"Error: {e.Message}");
        }
    }
}



/*
run:
  
Byte List: 174 234 234 75 

*/

 



answered Aug 4, 2025 by avibootz
edited Aug 4, 2025 by avibootz

Related questions

...