byte[] test = Form1.StrToByteArray(“simpletext”);
string encoded_text = BitConverter.ToString(test).Replace(“-“, “”).ToLowerInvariant();
textBox1.Text = encoded_text;//73696d706c6574657874
as from this line “73696d706c6574657874” to get back “simpletext” ??
//StrToByteArray()
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
Do you absolutely have to use hex to start with? One slightly more efficient (and reversible with framework methods) option would be to use base 64:
I personally wouldn’t suggest using ASCII as your encoding, however – UTF-8 will work the same way for ASCII characters, but allow all of Unicode to be encoded.
If you do have to use hex, you’ll need a method to parse hex – I have an example here.