Bob
Bob

Reputation: 783

C# Encoding from utf-16 to ascii

I get question marks in output of my program: ?????? ??????

 string str = "Привет медвед";
        Encoding srcEncodingFormat = Encoding.GetEncoding("utf-16");
        Encoding dstEncodingFormat = Encoding.ASCII;
        byte [] originalByteString = srcEncodingFormat.GetBytes(str);
        byte [] convertedByteString =  Encoding.Convert(srcEncodingFormat,
                                                       dstEncodingFormat, originalByteString);
        string finalString = dstEncodingFormat.GetString(convertedByteString);
        Console.WriteLine (finalString);

Upvotes: 1

Views: 4238

Answers (1)

Tom Blodget
Tom Blodget

Reputation: 20772

There is no text but encoded text. But, .NET's char and string use Unicode/UTF-16, as you know. So, you can simplify your code by calling GetBytes and passing in the string instead of doing it twice as your code does.

As for your question, you have a choice of a lossy conversion or no conversion at all. Below is code that prevents a lossy conversion.

Now, how to see the result? As with all text, it is a sequence of bytes. Your best bet is to write them to a file and open the file in an editor that you can indicate the encoding to and that can use a font that supports the characters you want to see.

string str = "Привет медвед";
Encoding dstEncodingFormat = Encoding.GetEncoding("US-ASCII", 
    new EncoderExceptionFallback(), 
    new DecoderReplacementFallback());
byte[] output = dstEncodingFormat.GetBytes(str);
File.WriteAllBytes("Test Привет медвед.txt", output);

Upvotes: 1

Related Questions