How to Convert UTF-8 Arabic letters to CodePage 1001?

别来无恙 提交于 2019-12-04 03:49:21

问题


I have a Star Micronics TSP that supports CodePage 1001 Arabic, how do I convert UTF-8 to that specific code page using C#?

Update: I found out that CodePage 864 is compatible with the printer, I tried sending hex values and I got the correct character,

myPrinter.PrintNormal(PrinterStation.Receipt, "\xFE8D");

I tried the following to convert a string to codePage 864:

Encoding enc = Encoding.GetEncoding(864);
byte[] arr = enc.GetBytes("السلام");

the byte arr values i'm getting after the encoding is {63,63,63,63,63,63} which is wrong in value and even the byte count is wrong because its a double byte character.


回答1:


Untested, but:

String s = Encoding.UTF8.GetString(bytes);
Encoding enc = Encoding.GetEncoding(1001);
byte[] arr2 = enc.GetBytes(s);

Of course, skip the first line if you are actually starting with a string, but since you mention UTF-8 I assumed binary.

Obviously for large data volumes you might use a TextReader/TextWriter (each with encoding) instead - but same idea.




回答2:


i don't think the original person who asked still needs the answer but this thread seems pretty famous and i don't want anyone to waste his time to find what i found the hard way..so

this answer relies primarily on the application provided with the Star Micronics TSP printer

key points: - reference "Interop.OposPOSPrinter_CCO.dll" - the printer object should be of type oposposprinter.printer and is initialized in a slightly different way (compared to opos.net) - the character codes are sent as a string and there's a variable to tell the printer object to use these decimal numbers as character codes

a sample VB.net project with a simple arabic letter converter can be found at https://bitbucket.org/loody/arabic-1001/overview

note: i don't have time to cover the rest of the letters/numbers



来源:https://stackoverflow.com/questions/6808522/how-to-convert-utf-8-arabic-letters-to-codepage-1001

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!