How to remove (?) While we are converting our byte content from unicode to ansi character

I need to convert Unicode characters to ansi characters

byte[] encode = Encoding.Convert(Encoding.Unicode, Encoding.Default, report);

      

I am using this piece of code. While I go through this, I found it to be an extra time? added symbol in first part

? FF EE 20 12

0


source to share


2 answers


It would be helpful if you post the input string as well as the output.

Encoding.Convert () will output '?' when it tries to convert a character to a source that doesn't have a matching character in the target encoding.



The sequence at the beginning of your output looks suspiciously close to the byte order mark (BOM). ANSI encodings do not have them, so if your Unicode stream has a BOM at the beginning, you can try to disable it before passing data to the converter.

+2


source


In this particular case, it looks like your input contains things that shouldn't be there (see Michael's answer ).



In general, when converting encodings, you can implement your own fallback encoding mechanism using theEncoderReplacementFallback

. You can easily force it to return empty for unsupported characters. Just supply Encoding

that uses your reserve when converting.

+1


source







All Articles