String to Binary in C#

String to Binary in C#

Here you go:

public static byte[] ConvertToByteArray(string str, Encoding encoding)
{
return encoding.GetBytes(str);
}

public static String ToBinary(Byte[] data)
{
return string.Join(" ", data.Select(byt => Convert.ToString(byt, 2).PadLeft(8, '0')));
}

// Use any sort of encoding you like.
var binaryString = ToBinary(ConvertToByteArray("Welcome, World!", Encoding.ASCII));

Convert a String to binary sequence in C#

Try this:

string str = "Hello"; 
byte []arr = System.Text.Encoding.ASCII.GetBytes(str);

Convert string to binary zeros and ones

Here is an example:

foreach (char c in "Test")
Console.WriteLine(Convert.ToString(c, 2).PadLeft(8, '0'));

How can i convert a string of characters into binary string and back again?

You can encode a string into a byte-wise representation by using an Encoding, e.g. UTF-8:

var str = "Out of cheese error";
var bytes = Encoding.UTF8.GetBytes(str);

To get back a .NET string object:

var strAgain = Encoding.UTF8.GetString(bytes);
// str == strAgain

You seem to want the representation as a series of '1' and '0' characters; I'm not sure why you do, but that's possible too:

var binStr = string.Join("", bytes.Select(b => Convert.ToString(b, 2)));

Encodings take an abstract string (in the sense that they're an opaque representation of a series of Unicode code points), and map them into a concrete series of bytes. The bytes are meaningless (again, because they're opaque) without the encoding. But, with the encoding, they can be turned back into a string.

You seem to be mixing up "ASCII" with strings; ASCII is simply an encoding that deals only with code-points up to 128. If you have a string containing an 'é', for example, it has no ASCII representation, and so most definitely cannot be represented using a series of ASCII bytes, even though it can exist peacefully in a .NET string object.

See this article by Joel Spolsky for further reading.

Binary to string/string to binary

        private void iTalk_Button_12_Click(object sender, EventArgs e)
{
ambiance_RichTextBox2.Text = BinaryToString(ambiance_RichTextBox1.Text);
//use what u need: BinaryToString or StringToBinary.
}

Convert String to Binary:

        public static string StringToBinary(string data)
{
StringBuilder sb = new StringBuilder();

foreach (char c in data.ToCharArray())
{
sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
}
return sb.ToString();
}

Convert Binary to String:

        public static string BinaryToString(string data)
{
List<Byte> byteList = new List<Byte>();

for (int i = 0; i < data.Length; i += 8)
{
byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
}

return Encoding.ASCII.GetString(byteList.ToArray());
}

Good luck!

turning a string with binary into binary in C#

Use Convert.ToInt32(string, int) where string is the string you want to convert, and int is the base of the number system you want to convert from, in your case base 2 or binary. Or if you really desperately need it to be a byte, then you can use Convert.ToByte(string, int). Like so:

using System;

class Program
{
public static void Main()
{
var input = Console.ReadLine(); // 01001010
var number = Convert.ToInt32(input, 2);
Console.WriteLine(number); // prints '74'
}
}

Be warned that Convert.ToXyz() will throw an exception of type FormatException if the given input string contains character that are illegal for the given base. For base 2 that would be any character that's not a 0 or 1, you might want to catch such exceptions, or check that all characters in the input string are either a '0' or '1' beforehand

C# how convert large HEX string to binary

You can just convert each hexadecimal digit into four binary digits:

string binarystring = String.Join(String.Empty,
hexstring.Select(
c => Convert.ToString(Convert.ToInt32(c.ToString(), 16), 2).PadLeft(4, '0')
)
);

You need a using System.Linq; a the top of the file for this to work.

Convert Hex string to Binary string C#

How about using String.PadLeft() ?

string value = "0x001";
string binary = Convert.ToString(Convert.ToInt32(value, 16), 2).PadLeft(12, '0');


Related Topics



Leave a reply



Submit