Can we simplify this string encoding code

后端 未结 3 586
刺人心
刺人心 2021-02-08 15:15

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

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

// conv         


        
相关标签:
3条回答
  • 2021-02-08 15:58

    Almost anything would be cleaner than this - you're really abusing text here, IMO. You're trying to represent effectively opaque binary data (the encoded text) as text data... so you'll potentially get things like bell characters, escapes etc.

    The normal way of encoding opaque binary data in text is base64, so you could use:

    return Convert.ToBase64String(Encoding.GetEncoding(936).GetBytes(text));
    

    The resulting text will be entirely ASCII, which is much less likely to cause you hassle.

    EDIT: If you need that output, I would strongly recommend that you represent it as a byte array instead of as a string... pass it around as a byte array from that point onwards, so you're not tempted to perform string operations on it.

    0 讨论(0)
  • 2021-02-08 16:00

    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 讨论(0)
  • 2021-02-08 16:21

    Does your receipt printer have an API that accepts a byte array rather than a string? If so you may be able to simplify the code to a single conversion, from a Unicode string to a byte array using the encoding used by the receipt printer.

    Also, if you want to convert an array of bytes to a string whose character values correspond 1-1 to the values of the bytes, you can use the code page 28591 aka Latin1 aka ISO-8859-1.

    I.e., the following

    foreach (byte b in converted) 
        builder.Append((char)b); 
    
    string result = builder.ToString(); 
    

    can be replaced by:

    // All three of the following are equivalent
    // string result = Encoding.GetEncoding(28591).GetString(converted);
    // string result = Encoding.GetEncoding("ISO-8859-1").GetString(converted);
    string result = Encoding.GetEncoding("Latin1").GetString(converted);
    

    Latin1 is a useful encoding when you want to encode binary data in a string, e.g. to send through a serial port.

    0 讨论(0)
提交回复
热议问题