FOLLOW US
softpcapps Software CODE HELP BLOG



Freeware Open Source Windows Software Applications and Free Online Tools

For Windows 11 : Download and run .NET 3.5 Installer if the application is not running

How to Convert String to HEX

To convert string to HEX (hexademical string) you can do the following :
	

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

		var bytes=enc.GetBytes(txt));

		var sb = new StringBuilder();

		// if we shold add spaces between the hexademical values
		bool addspaces=true;

		foreach (var t in bytes)
		{
			sb.Append(t.ToString("X2"));

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

		string str=sb.ToString();

		return str;

	}