locked
Unicode Byte to string RRS feed

  • Question

  • I need to print some greek characters like αβδ from my C# program. By using Hex Editor, I know their HEX (or byte) value is CEB1, CEB2 and CEB3.

    So now, how should I construct the data and print it from my C# program? I can't do this anyway.

    String temp = "" + 0xCEB1 + 0xCEB2 + 0xCEB3;

    It printed out as some numeric value.

    Ps. I don't want to hardcode the αβδ characters into my source code.

    What can I do? Please help me. Urgent.

    I will grade you. Promise.

    Thanks

    Thursday, September 7, 2006 7:16 AM

Answers

  • It seams as your printer want the data UTF8 encoded.

    You can use the class System.Text.Encoding to transform the unicode strings into a sqeuence of bytes. The resulting byte array can now be sent to the printer.

    Some background Info:
    The byte sequence that represents a character depends on the encoding. For instance:
    Char: α
    Unicode (UTF16) -> 0x03B1
    UTF8 -> 0xCEB1

    The full details about unicode is available online on www.unicode.org.

    Hope this helps

    Thursday, September 7, 2006 5:39 PM

All replies

  • You can write them with a unicode escape sequence

    string temp = "\uCEB1\uCEB2\uCEB3";

     

    Thursday, September 7, 2006 7:59 AM
  • Hi MVP,

    It doesn't work. When I printed it out, it appeared as ? ? ?

    However, I found something very strange.

    When I opened HEX editor, I found that values of the data are CEB1 CEB2 CEB3 for αβδ

    But, from characters map (in start->accessories->system tools), the unicode of these values are actually 03B1, 03B2, 03B3.

    But again, in order to send to my printer for printing, the values must CEB1,....but it somehow appeared as ? ? ? from my C# program's output.

    Very confusing to me. Please help. Thanks

    Thursday, September 7, 2006 9:00 AM
  • It seams as your printer want the data UTF8 encoded.

    You can use the class System.Text.Encoding to transform the unicode strings into a sqeuence of bytes. The resulting byte array can now be sent to the printer.

    Some background Info:
    The byte sequence that represents a character depends on the encoding. For instance:
    Char: α
    Unicode (UTF16) -> 0x03B1
    UTF8 -> 0xCEB1

    The full details about unicode is available online on www.unicode.org.

    Hope this helps

    Thursday, September 7, 2006 5:39 PM
  • Hi MVP,

    Thanks for your answer. I am getting closer to the answer I wanted.

    Other than the puzzle you have solved for me above, I also found another mistake I made in my printer class. Below is the original code:

    -----------------------------------------------------------------------------------------------------------------

    IntPtr pBytes;

    Int32 dwCount;

    // How many characters are in the string?

    dwCount = szString.Length;

    // Assume that the printer is expecting ANSI text, and then convert the string to ANSI text.

    pBytes = Marshal.StringToCoTaskMemAnsi(szString);

    ---------------------------------------------------------------------------------------------------------------------------------------------------

    As you can see, since my data contains UTF-8 data, then I shouldn't use StringToCoTaskMemAnsi(). I guess it will corrupt the data after it is converted to ANSI? But I am not sure how to convert szString to IntPtr and maintaining the UTF-8 data.

    Please kindly advice. I believe this will finally stop my nightmare if I can solve this. Thanks

    Friday, September 8, 2006 2:34 AM