Created
November 25, 2015 06:40
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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