.NET: How do I know if the encoding supports all characters in my string?

I have a lot of text that I need to display, including all kinds of characters from many languages. Sometimes I need to output text to a character encoding other than Unicode (like Shift-JIS or ISO-8859-2) to match the page it will be on.

If there are characters in the text that the encoding cannot process (for example, Japanese characters in the ISO-8859-2 encoded output), I get odd characters in the output. I can avoid them, but I would prefer to do this only if it is really necessary.

So my question is this: is there a way I can tell in advance if the encoding can handle all the characters in my string?

EDIT: I think EncoderFallback is probably the correct answer to the question I asked. Unfortunately, this does not work in my specific situation. My thought was to convert characters to their equivalent HTML objects (e.g. & # 12514 instead of モ). However, the encoder only converts the first such character that it finds, and if I set Response.ContentEncoding, it never calls my EncoderFallback at all.

+3
source share
4 answers

You can write your own EncoderFallback class assigned to the encoder before encoding.

, (, , , ).

Fallback , .

+5

, EncoderFallback EncoderExceptionFallback. :.

Encoding e= Encoding.GetEncoding(932, new EncoderExceptionFallback(), new DecoderExceptionFallback());

EncoderFallbackException, GetBytes().

+3

, ?

Encoding.GetBytes() Encoding.GetStrings() .

.

0

, . ( EncoderFallback .) , .

, , encoding.GetEncoder(). Convert Encoder, , , .

, , , .

0

Source: https://habr.com/ru/post/1704514/


All Articles