Skip to content

Instantly share code, notes, and snippets.

@aaronhoffman
Created February 23, 2016 00:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronhoffman/ad16b27d14e2f5d7e16c to your computer and use it in GitHub Desktop.
Save aaronhoffman/ad16b27d14e2f5d7e16c to your computer and use it in GitHub Desktop.
Convert SQL Server Management Studio SSMS Varbinary Column Output to Byte Array C#
static void Main(string[] args)
{
// read input from local file (could also pass in via args)
var hexInput = File.ReadAllText("hexInput.txt");
// convert to binary, strip off first two chars 'Ox'
var byteArray = StringToByteArray(hexInput.Substring(2));
// write bytes to file
File.WriteAllBytes("output.file", byteArray);
}
public static byte[] StringToByteArray(string hex)
{
int cnt = hex.Length;
byte[] bytes = new byte[cnt / 2];
for (int i = 0; i < cnt; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment