Encoding conversion error

I have a little problem with changing the encoding of a string. Actually I am reading DB strings that are encoded using code page 850 and I have to prepare them to be suitable for a WCF compliant service.

From DB I am reading characters \ x10 and \ x11 (triangle shapes) and I want to convert them to Unicode format to prevent serialization / deserialization issue during WCF call. (The characters and are not valid according to the XML specifications, even though WCF serializes them.)

Now I am using the following code to covertly encode strings, but nothing happens. The result string is virtually identical to the original one.

I probably missed something ...

Please help me!!!

Emanuele

 static class UnicodeEncodingExtension
    {
        public static string Convert(this Encoding sourceEncoding, Encoding targetEncoding, string value)
        {
            string reEncodedString = null;

            byte[] sourceBytes = sourceEncoding.GetBytes(value);
            byte[] targetBytes = Encoding.Convert(sourceEncoding, targetEncoding, sourceBytes);
            reEncodedString = sourceEncoding.GetString(targetBytes);

            return reEncodedString;
        }

    }

    class Program
    {
        private static Encoding Cp850Encoding = Encoding.GetEncoding(850);
        private static Encoding UnicodeEncoding = Encoding.UTF8;

        static void Main(string[] args)
        {
            string value;
            string resultValue;
            value = "\x10";
            resultValue = Cp850Encoding.Convert(UnicodeEncoding, value);

            value = "\x11";
            resultValue = Cp850Encoding.Convert(UnicodeEncoding, value);

            value = "\u25b6";
            resultValue = UnicodeEncoding.Convert(Cp850Encoding, value);

            value = "\u25c0";
            resultValue = UnicodeEncoding.Convert(Cp850Encoding, value);

        }

    }

      

+1


a source to share


5 answers


All strings stored in a string are actually Unicode.Unicode. Read: Strings in .Net and C # and The Absolute Minimum of All Programs A developer absolutely, positively needs to know about Unicode and character sets (no excuses!)

Edit: I suppose you want the Convert function to automatically change \ x11 to \ u25c0, but the problem here is that \ x11 is valid in almost any encoding, the differences usually start with the \ x80 character, so the Convert function will support it. even if you do this:



string reEncodedString = null;
byte[] unicodeBytes = UnicodeEncoding.Unicode.GetBytes(value);
byte[] sourceBytes = Encoding.Convert(Encoding.Unicode,
                                sourceEncoding, unicodeBytes);

      

You can see the mappings from CP850 to Unicode at unicode.org . So, for this conversion, you will have to manually change these characters.

0


a source


You seem to think there is a problem based on a misunderstanding. But jmservera is correct - all strings in .NET are encoded internally as unicode.

You haven't said exactly what you want to achieve. Are you having trouble on the other end of the wire?



Just FYI, you can set the text encoding in the WCF binding using the textMessageEncoding element in the config file.

0


a source


I suspect this line may be your culprit

reEncodedString = sourceEncoding.GetString(targetBytes);

      

which seems to take your target encoded byte string and ask your sourceEncoding to infer a string from them. I haven't had a chance to test it, but I suspect the following might be better

reEncodedString = targetEncoding.GetString(targetBytes);

      

0


a source


  • byte[] sourceBytes =Encoding.Default.GetBytes(value)

  • Encoding.UTF8.GetString(sourceBytes)

this sequence is useful for loading a unicode file from a service (for example, an xml file that contains a Persian character)

0


a source


You should try the following:

byte[] sourceBytes = sourceEncoding.GetBytes(value);
var convertedString = Encoding.UTF8.GetString(sourceBytes);

      

0


a source







All Articles