views:

212

answers:

2

I need to convert the unicode characters to ansi characters

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

I use this piece of code. While I am viewing this I found that extra ? character is added in the first part

?FF EE 20 12

+2  A: 

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

Encoding.Convert() will output a '?' when it tries to convert a character in the source that does not have a corresponding character in the target encoding.

The sequence at the start of your output looks suspiciously close to a Byte Order Mark (BOM). ANSI codepages don't have these, so if your Unicode stream has a BOM at the start you might try stripping it off before passing the data to the converter.

Michael Burr
+1  A: 

In this particular case, it looks like your input data contains stuff that shouldn't be in there (cf. Michael's answer).

In the general case, when converting between encodings, you can implement your own encoding fallback mechanism using the EncoderReplacementFallback class. You could easily make it return empty for unsupported characters. Simply supply an Encoding that uses your fallback when converting.

bzlm