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

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

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,641 questions

55,376 answers

573 users

How to convert text to binary code in C#

1 Answer

0 votes
using System;

class Program
{
    static string TextToBin(string txt) {
        string bin = string.Empty;

        foreach (char ch in txt) {
            // Convert character to ASCII value
            int ascii = (int)ch;

            // Convert ASCII value to binary string
            string binary = Convert.ToString(ascii, 2);

            // Pad binary string to ensure it is 8 bits long
            binary = binary.PadLeft(8, '0');

            // Append binary string to result
            bin += binary + " ";
        }

        return bin.Trim(); // Remove trailing space
    }

    static void Main(string[] args)
    {
        string str = "CSharp";
        
        string binaryResult = TextToBin(str);

        Console.WriteLine(binaryResult);
    }
}


/*
run:

01000011 01010011 01101000 01100001 01110010 01110000

*/

 



answered Apr 13, 2025 by avibootz
...