I'm trying to find out how to convert input to a console, into binary; how can such a conversion be made in C#?
Thank you in advance.
string s = Console.ReadLine();
byte[] bytes = Encoding.ASCII.GetBytes(s);
Note that the encoding used by the console isn't actually ASCII... you should probably use Console.InputEncoding
instead of Encoding.ASCII
To get the binary representation of each byte, you can use Convert.ToString
:
foreach(byte b in bytes)
{
Console.WriteLine(Convert.ToString(b, 2));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With