Skip to content

Instantly share code, notes, and snippets.

@rodelta
Last active February 23, 2018 23:07
Show Gist options
  • Save rodelta/0c3ce89cc1a9ff9f6587a4981593c57b to your computer and use it in GitHub Desktop.
Save rodelta/0c3ce89cc1a9ff9f6587a4981593c57b to your computer and use it in GitHub Desktop.
Takes a hex string of arbitrary value and turns it into a binary string. Works with hex strings that would overflow number types (e.g. EPC codes).
/* Creates a binary string from a arbitrary long hex string
Taken from https://github.com/dannyhaak/TagDataTranslation/
*/
private static string HexToBinary(string hex)
{
StringBuilder binary = new StringBuilder();
for (int i = 0; i < hex.Length; i++)
{
int integer = Convert.ToInt16(hex.Substring(i, 1), 16);
binary.Append(Convert.ToString(integer, 2).PadLeft(4, '0'));
}
return binary.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment