FOLLOW US
softpcapps Software CODE HELP BLOG

Shareware and free Open Source Windows Software Applications and free Online Tools

How to Convert String to Binary

To convert a String to Binary you can do the following :
	

	public string StringToBinary(string txt)
	{ 	
		// use your desired encoding here
		Encoding enc = System.Text.Encoding.UTF8;

		var bytes = enc.GetBytes(txt);

		var sb = new StringBuilder();

		// if we should add spaces between the binary values
		bool addspaces=true;

		foreach (var t in bytes)
		{
			sb.Append(Convert.ToString(t, 2).PadLeft(8, '0'));

			if (addspaces) sb.Append(" ");
		}

		string str = sb.ToString();

		return str;
	}