With # ASCII GetBytes, how to set which character is used for unrecognizable conversion?

I am porting some code from C ++ to C # and I need to do the following:

ASCII.GetByteswhen he encounters a unicode character, he does not recognize it, returns me a character with decimal number 63 (question mark), but in my C ++ code, using WideCharToMultiByte(CP_ACP, ...when it encounters a character that does not know that it uses a character with decimal number 37 (% sign).

My question is: how can I get ASCII.GetBytes to return to me # 37 instead of # 63 for unknown characters?

+3
source share
2 answers

# DecoderFallback/EncoderFallback , , . Encoding.ASCII, , . :

using System;
using System.Text;

class Test
{    
    static void Main()
    {
        Encoding asciiClone = (Encoding) Encoding.ASCII.Clone();
        asciiClone.DecoderFallback = new DecoderReplacementFallback("%");
        asciiClone.EncoderFallback = new EncoderReplacementFallback("%");

        byte[] bytes = { 65, 200, 66 };
        string text = asciiClone.GetString(bytes);
        Console.WriteLine(text); // Prints A%B
        bytes = asciiClone.GetBytes("A\u00ffB");
        Console.WriteLine(bytes[1]); // Prints 37
    }
}
+6

++ WideCharToMultiByte lpDefaultChar = "%".

Encoding.GetBytes, WideCharToMultiByte P/Invoke.

0

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


All Articles