I'm trying to create a QR barcode in .net using IronBarcode. I'm following their tutorial for writing binary data, but it's not working with Arabic text. It works great with English, but with Arabic the console output is "?????????????????". I've tried UTF8, UTF7 but nothing is working. https://ironsoftware.com/csharp/barcode/tutorials/csharp-qr-code-generator/#reading-and-writing-binary-data
My code is:
using IronBarCode;
var Msg = "مرحبا";
byte[] BinaryData = System.Text.Encoding.UTF8.GetBytes(Msg);
var bitmap = IronBarCode.QRCodeWriter.CreateQrCode(BinaryData,500, IronBarCode.QRCodeWriter.QrErrorCorrectionLevel.Highest).ToBitmap();
var barcodeResult = IronBarCode.BarcodeReader.ReadASingleBarcode(bitmap);
var stringResult = System.Text.Encoding.UTF8.GetString(barcodeResult.BinaryValue);
Console.WriteLine(stringResult) // outputs '?????'
Please can someone advise what I'm missing?
CodePudding user response:
var Msg = "هذا لا يعمل";
byte[] BinaryData = System.Text.Encoding.UTF8.GetBytes(Msg);
var bitmap = IronBarCode.QRCodeWriter.CreateQrCode(BinaryData, 500, IronBarCode.QRCodeWriter.QrErrorCorrectionLevel.Highest).ToBitmap();
var barcodeResult = IronBarCode.BarcodeReader.ReadASingleBarcode(bitmap);
var stringResult =
System.Text.Encoding.UTF8.GetString(barcodeResult.BinaryValue);
// stringResult is "هذا لا يعمل"
The issue you are seeing is that Console.WriteLine doesn’t support Arabic characters in Visual Studio. It prints "?????" for all non-Roman characters.
You can verify the correct result by saving to file, or putting in a debugging breakpoint and hover over the variable in Visual Studio.
This video explains the issue, and also has a work-around: https://m.youtube.com/watch?v=rTqBnJ8HrSc
Console.OutputEncoding = System.Text.Encoding.Unicode;