How to encode extended ASCII characters (bytes 128-255) ISO-8859-1 in USS Code 128 Barcode Symbology

USS Code 128 is a well known barcode symbology standard (ISO/IEC 15417:2007) widely implemented in many applications where a relatively large amount of data must be encoded in a relatively small amount of space.

Code 128 is a linear bar code symbology capable of encoding the full 128 ASCII character set (bytes 0-127) but it also was designed for encoding Extended Characters (bytes 128-255) by using an special artifact called Function Character (a.k.a. FNC).

The most known Function Character is FNC1 which is used by the GS1-128 (formerly UCC/EAN-128) barcode symbolgy for encoding Application Identifiers (Ais). USS Code 128 features four Function Characters (FNC1, FNC2, FNC3, and FNC4). The last one, FNC4 is the one you need to use if you want to encode Extended Characters i.e. bytes 128 to 255. By default, the reference character set for the Extended Character is the corresponding of ISO 8859-1, Latin alphabet 1, but application specifications may define or reference alternative sets.

USS Code 128 Function 4 Character (FNC4) works like the following:

  • If a single FNC4 is used, then the value 128 is added to the ASCII value of the following data character (0-127) in the symbol. Subsequent data characters revert to the standard ASCII set.
  • If two consecutive FNC4s are used, the value 128 is added to the ASCII value of the following data characters until two further consecutive FNC4s are found or the end of the symbol is reached. If, during this sequence of extended ASCII encodation, a single FNC4 is found, it is used to revert to standard ASCII encodation for the next data character only.

Neodynamic Barcode Professional products line (.NET Windows Forms, ASP.NET, SQL Reporting Services, SDK, WPF, and Silverlight) do support all Code 128 Function Characters and you can encode the extended characters of ISO-8859-1 by using the following approach.

Here you are a simple class method you can add to your .NET project for encoding extended characters using USS Code 128 symbology with Neodynamic Barcode Professional dll components.

NOTE: This class method just use the Single FNC4 encoding for simplicity but you can modify it for using two consecutive FNC4s as described above.

C#

private string EncodeExtendedCharsInCode128(string text)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    //get bytes from text
    byte[] chars = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(text);

    //analyze bytes for encoding extended chars using FNC4 in Code 128 symbology
    for (int i = 0; i  127) //encode char using FNC4
        {
            sb.Append(BarcodeProfessional.CODE128_FNC4);
            sb.Append((char)(chars[i] - 128));
        }
        else //encode char in ASCII mode
        { 
            sb.Append((char)chars[i]);
        }
    }

    return sb.ToString();
}

VB.NET

Private Function EncodeExtendedCharsInCode128(ByVal text As String) As String
    Dim sb As New System.Text.StringBuilder()
	
    'get bytes from text
    Dim chars As Byte() = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(text)

    'analyze bytes for encoding extended chars using FNC4 in Code 128 symbology
    For i As Integer = 0 To chars.Length - 1
        If chars(i) > 127 Then
            'encode char using FNC4
            sb.Append(BarcodeProfessional.CODE128_FNC4)
            sb.Append(ChrW(chars(i) - 128))
        Else
            'encode char in ASCII mode
            sb.Append(ChrW(chars(i)))
        End If
    Next

    Return sb.ToString()
End Function

You can use that method in your class for setting up the Code property of the BarcodeProfessional class with the value to encode including extended characters like follows:

C#

this.BarcodeProfessionalObj.Symbology = Symbology.Code128;
this.BarcodeProfessionalObj.Code = this.EncodeExtendedCharsInCode128("Ext Chars Æ Ø ñ û");

VB.NET

Me.BarcodeProfessionalObj.Symbology = Symbology.Code128
Me.BarcodeProfessionalObj.Code = Me.EncodeExtendedCharsInCode128("Ext Chars Æ Ø ñ û")

Advertisements

One Response to How to encode extended ASCII characters (bytes 128-255) ISO-8859-1 in USS Code 128 Barcode Symbology

  1. Pingback: How to perform message append in USS Code 128 barcodes with FNC2 « Neodynamic's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: