bardecode.com

  • Increase font size
  • Default font size
  • Decrease font size
Home Knowledge Base Handling Unicode encoded barcodes in VB.Net

Handling Unicode encoded barcodes in VB.Net

2-D barcode formats such as PDF-417 can encode a huge character set, which means that the barcode reader toolkit needs to encode characters outside the normal ASCII range. If the Encoding property is set to 2 then the toolkit will use Unicode encoding, whereby characters can be represented by the string &#N; where N is an integer.

For example:

Ñ represents the character Ñ

To decode this to a unicode string in VB.Net you need to process the barcode value through a regular expression replacement as follows:

Step 1:

Create a function to convert the integer values (stored in a string) to a single character string holding the character represented by the unicode integer value...

Private Function MatchHandler(ByVal m As System.Text.RegularExpressions.Match) As String
    Dim i As Int16
    i = Int16.Parse(m.Groups(1).Value, Globalization.NumberStyles.Integer)
    ' Suppress null characters.
    If i = 0 Then
        Return ""
    End If
    Return Chr(i).ToString()
End Function

Step 2:

In the function where you read the barcode values:

' Get the barcode value
strBarcode = barcode.GetBarString(1)
' Create the delegate and the regular expression
Dim myDelegate As New System.Text.RegularExpressions.MatchEvaluator(AddressOf MatchHandler)
Dim re As New System.Text.RegularExpressions.Regex("&#([0-9]+);")
' Convert the Unicode escape sequences to a regular unicode string
strBarcode = re.Replace(strBarcode, myDelegate)