3

Say, when I convert a string into a byte array using single-byte encoding, some characters will be replaced with '?':

string strData="©";
byte[] bytesData = Encoding.ASCII.GetBytes(strData);

Is there any way to find out if a string will lose some of it's data if I convert it to ANSI?

PS. I'm not inquiring about benefits of Unicode encodings, such as UTF-8.

slugster
  • 49,403
  • 14
  • 95
  • 145
ahmd0
  • 16,633
  • 33
  • 137
  • 233

2 Answers2

6

How about?

Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(strData)) == strData;
DanielB
  • 19,910
  • 2
  • 44
  • 50
3

If you instantiate your own Encoder you can specify an EncoderFallback, one of the pre-defined such objects is EncoderFallback.ExceptionFallback which will:

throws an exception when an input character cannot be encoded

Richard
  • 106,783
  • 21
  • 203
  • 265