According to this snippet here, this approach should be good for long strings:
private string StringToHex(string hexstring)
{
StringBuilder sb = new StringBuilder();
foreach (char t in hexstring)
{
//Note: X for upper, x for lower case letters
sb.Append(Convert.ToInt32(t).ToString("x"));
}
return sb.ToString();
}
usage:
string result = StringToHex("Hello world"); //returns "48656c6c6f20776f726c64"
Another approach in one line
string input = "Hello world";
string result = String.Concat(input.Select(x => ((int)x).ToString("x")));