Search code examples
c#barcodezxing

ZXing.Net PDF417 Barcode from HEX


Is it possible to build a PDF417 barcode from a HEX? I tried something with ZXing, but it wont work with an encoded string in my case.

HEX: fe-30-09-33-31-37-30-31-30-32-30-31-f9-20-01-34-fe-30-01-20-fc-20-06

Other generators can do this (https://stackoverflow.com/a/39471232/3236231), but now this solution costs a few thousand dollars. ZXing fits all my needs, but I cant find a propper way of using my data.


Solution

  • the following code snippet should work as expected:

      [Test]
      public void Hex2Pdf417()
      {
         var hexStr = "fe3009333137303130323031f9200134fe300120fc2006";
         var byteArray = Enumerable.Range(0, hexStr.Length / 2).Select(x => Convert.ToByte(hexStr.Substring(x * 2, 2), 16)).ToArray();
         var byteArrayAsString = new String(byteArray.Select(b => (char)b).ToArray());
    
         // encode the string as PDF417
         var writer = new BarcodeWriter
         {
            Format = BarcodeFormat.PDF_417,
            Options = new PDF417EncodingOptions
            {
               Height = 200,
               Width = 200,
               Margin = 10
            }
         };
         var bitmap = writer.Write(byteArrayAsString);
    
         // try to decode the PDF417
         var reader = new BarcodeReader
         {
            Options = new DecodingOptions
            {
               PossibleFormats = new List<BarcodeFormat>
               {
                  BarcodeFormat.PDF_417
               },
               PureBarcode = true
            }
         };
         var result = reader.Decode(bitmap);
    
         // make sure, the result is the same as the original hex
         var resultBackToBytes = result.Text.Select(c => (byte)c).ToArray();
         var resultAsHexString = String.Join("", resultBackToBytes.Select(b => b.ToString("x2")));
    
         Assert.That(resultAsHexString, Is.EqualTo(hexStr));
      }