Created
February 23, 2016 00:58
-
-
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#
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
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