How to encode UTF-8 text into QR Code barcodes using VB.NET or C#
Product Barcode Professional .NET Product Line Published 01/02/2018 Updated 01/02/2018
Overview
In this guide you will learn how to generate QR Code barcode images encoding UTF-8 strings like Arabic, Hebrew, Chinese alphabets by using Barcode Professional .NET Product Line
Requirements
Neodynamic Barcode Professional SDK for .NET
Microsoft .NET Framework 2.0 or greater
Microsoft Visual Studio
The following sample code, we'll encode the string Hellow World in Arabic language which (based on Google Translate service :) it's reprenseted by this string: مرحبا بالعالم
To reproduce the sample app, please follow these steps:
In this article Barcode Professional SDK for .NET package is being used by you can adapt it to any of our others Barcode Professional .NET Product Line for platforms like .NET CORE, Reporting Services (SSRS), WPF/Windows Forms & ASP.NET MVC/WebForms
Follow up these steps
- Open Visual Studio and create a new Console app using your preferred .NET language VB or C#
- In your project add a reference to Neodynamic.SDK.Barcode.dll
- In the main method, copy and paste the following code
VB
'The UTF-8 string to be encoded Dim valueToEncode As String = "مرحبا بالعالم" Using bc As new BarcodeProfessional() 'QR Code settings bc.Symbology = Symbology.QRCode bc.QRCodeEncoding = QRCodeEncoding.Byte bc.QRCodeByteEncodingName = "utf-8" bc.QRCodeProcessTilde = True 'convert each char to hex representation for Tilde Processing 'https://neodynamic.com/barcodes/QR-Code-Barcode.aspx#ProcessTilde Dim sb As New System.Text.StringBuilder() 'Start appending UTF-8 BOM sb.Append("~hEF~hBB~hBF") 'Append rest of text encoded as UTF-8 bytes Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(valueToEncode) For i As Integer = 0 To buffer.Length -1 sb.Append("~h") sb.Append(Convert.ToString(buffer(i), 16).PadLeft(2, "0"C)) Next 'Set value to encode bc.Code = sb.ToString() bc.Save("c:\temp\QR.png", System.Drawing.Imaging.ImageFormat.Png) End Using
C#
//The UTF-8 string to be encoded string valueToEncode = "مرحبا بالعالم"; using (BarcodeProfessional bc = new BarcodeProfessional()) { //QR Code settings bc.Symbology = Symbology.QRCode; bc.QRCodeEncoding = QRCodeEncoding.Byte; bc.QRCodeByteEncodingName = "utf-8"; bc.QRCodeProcessTilde = true; //convert each char to hex representation for Tilde Processing //https://neodynamic.com/barcodes/QR-Code-Barcode.aspx#ProcessTilde System.Text.StringBuilder sb = new System.Text.StringBuilder(); //Start appending UTF-8 BOM sb.Append("~hEF~hBB~hBF"); //Append rest of text encoded as UTF-8 bytes byte[] buffer = System.Text.Encoding.UTF8.GetBytes(valueToEncode); for (int i = 0; i < buffer.Length; i++) { sb.Append("~h"); sb.Append(Convert.ToString(buffer[i], 16).PadLeft(2, '0')); } //Set value to encode bc.Code = sb.ToString(); bc.Save(@"c:\temp\QR.png", System.Drawing.Imaging.ImageFormat.Png); }
- That's it! Run the app, the QR Code barcode encoding the UTF-8 string should be generated. Try decoding it by using a barcode reader that supports UTF-8 strings. For an online decoder you could try ZXing