Skip to content

Instantly share code, notes, and snippets.

@lisardggY
Created November 25, 2015 06:40
Show Gist options
  • Save lisardggY/b68a737daf3e4001f31e to your computer and use it in GitHub Desktop.
Save lisardggY/b68a737daf3e4001f31e to your computer and use it in GitHub Desktop.
Encodes a string into a base16/hexadecimal string format. Useful for maintaining case sensitivity where case sensitivity cannot be maintained, like a filename or URL.
using System.Text;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
public static string ToHexBytes(this string plainText, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
var bytes = encoding.GetBytes(plainText);
return new SoapHexBinary(bytes).ToString();
}
public static string FromHexBytes(this string hexText, Encoding encoding = null)
{
encoding = encoding ?? Encoding.UTF8;
var bytes = SoapHexBinary.Parse(hexText).Value;
return encoding.GetString(bytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment