Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 544393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:39:00+00:00 2026-05-13T10:39:00+00:00

Is it possible to simplify this code into a cleaner/faster form? StringBuilder builder =

  • 0

Is it possible to simplify this code into a cleaner/faster form?

StringBuilder builder = new StringBuilder();
var encoding = Encoding.GetEncoding(936);

// convert the text into a byte array
byte[] source = Encoding.Unicode.GetBytes(text);

// convert that byte array to the new codepage. 
byte[] converted = Encoding.Convert(Encoding.Unicode, encoding, source);

// take multi-byte characters and encode them as separate ascii characters 
foreach (byte b in converted)
    builder.Append((char)b);

// return the result
string result = builder.ToString();

Simply put, it takes a string with Chinese characters such as 鄆 and converts them to ài.

For example, that Chinese character in decimal is 37126 or 0x9106 in hex.

See http://unicodelookup.com/#0x9106/1

Converted to a byte array, we get [145, 6] (145 * 256 + 6 = 37126). When encoded in CodePage 936 (simplified chinese), we get [224, 105]. If we break this byte array down into individual characters, we 224=e0=à and 105=69=i in unicode.

See http://unicodelookup.com/#0x00e0/1
and
http://unicodelookup.com/#0x0069/1

Thus, we’re doing an encoding conversion and ensuring that all characters in our output Unicode string can be represented using at most two bytes.

Update: I need this final representation because this is the format my receipt printer is accepting. Took me forever to figure it out! 🙂 Since I’m not an encoding expert, I’m looking for simpler or faster code, but the output must remain the same.

Update (Cleaner version):

return Encoding.GetEncoding("ISO-8859-1").GetString(Encoding.GetEncoding(936).GetBytes(text));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:39:00+00:00Added an answer on May 13, 2026 at 10:39 am

    Well, for one, you don’t need to convert the “built-in” string representation to a byte array before calling Encoding.Convert.

    You could just do:

    byte[] converted = Encoding.GetEncoding(936).GetBytes(text);
    

    To then reconstruct a string from that byte array whereby the char values directly map to the bytes, you could do…

    static string MangleTextForReceiptPrinter(string text) {
        return new string(
            Encoding.GetEncoding(936)
                .GetBytes(text)
                .Select(b => (char) b)
                .ToArray());
    }
    

    I wouldn’t worry too much about efficiency; how many MB/sec are you going to print on a receipt printer anyhow?

    Joe pointed out that there’s an encoding that directly maps byte values 0-255 to code points, and it’s age-old Latin1, which allows us to shorten the function to…

    return Encoding.GetEncoding("Latin1").GetString(
               Encoding.GetEncoding(936).GetBytes(text)
           );
    

    By the way, if this is a buggy windows-only API (which it is, by the looks of it), you might be dealing with codepage 1252 instead (which is almost identical). You might try reflector to see what it’s doing with your System.String before it sends it over the wire.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Any way to simplify this code? What is the best way to
Is it possible to simplify this? public void setDisabled(boolean disabled) { if(disabled) this._rflags |=
I would like to simplify my code and add the possible to the css
Is there a way to simplify this working code? This code gets for an
I've recently tried to simplify some of my code by using generics where possible.
Is there any way to simplify this code? Or any way to create another
I have this code snippet that I want to simplify: switch (typeString) { case
I am looking for a Pythonic way to simplify this code: fix = re.compile(r'((?<=>\n)(\t){2}(?=<))')
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.