HexToByte Conversion

The following is a generic routine to convert hexadecimal data to byte data. The byte data can then be written, using the BinaryWriter class in Visual Studio.

Sample Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
try
{
TAeClienExport[] exports = ws.getSystemExportsBasic();

string outData = ws.executeExportOperation(exports[0]);
byte[] byteData = HexToByte(outData.Substring(2));

string filename = "exports.csv";
BinaryWriter binWriter = new BinaryWriter(File.Open(filename, FileMode.Create));

binWriter.Write(byteData);
binWriter.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

return;

private static byte[] HexToByte(string hex)
{
byte[] raw = new byte[hex.Length / 2];
for (int i = 0; i < raw.Length; i++)
{
raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return raw;
}