Reading binary data from a barcode using Visual C#
If you are handling binary data in barcodes such as you find in some 2-D barcodes (PDF417, QrCode or DataMatrix) then the following method will allow you to create a byte array that contains the original binary data from the barcode – including all the null and 8-bit values.
1. Using Quoted printable Encoding
barcode.Encoding = 1;
This will return the barcode value using either printable characters or =XX where XX is a hex value for non-printable characters (e.g for null you get =00). By default the data will be potentially be handled as if it contains utf-8 encoded data.
2. Convert the quoted printable output from GetBarString to a byte array using the following function:
private byte[] convertQPToByteArray(string qpString)
{
// Assess how many bytes required
int c = 0;
for (int i = 0; i < qpString.Length; i++, c++)
if (qpString[i] == '=')
i += 2;
byte[] binaryData = new byte[c];
int zero = Convert.ToInt16('0');
c = 0;
for (int i = 0; i < qpString.Length; i++, c++)
{
if (qpString[i] == '=')
{
binaryData[c] = (byte) int.Parse(qpString.Substring(i + 1, 2), System.Globalization.NumberStyles.HexNumber);
i += 2;
}
else
{
binaryData[c] = Convert.ToByte(qpString[i]);
}
}
return binaryData;
}