ahmd0
ahmd0

Reputation: 17293

Can I find out if string to ANSI conversion will result in loss of data?

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.

Upvotes: 3

Views: 489

Answers (2)

Richard
Richard

Reputation: 109080

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

Upvotes: 3

DanielB
DanielB

Reputation: 20240

How about?

Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(strData)) == strData;

Upvotes: 6

Related Questions