C# string.Replace for removing �?

Viewed 132

I am converting hex to a UTF8 string using below line.

var obInstruction = Encoding.UTF8.GetString(ob.Bits);

In the result I got � between every character as shown in the picture below. what is � ?

what is �

So I added replace to the line and changed it to var obInstruction = Encoding.UTF8.GetString(ob.Bits).Replace("�", ""); but � won't go away.

When I tried to replace other characters Replace work fine but not for �.

What is � and how can I remove it?

In Power query, Text.Clean will remove such strange characters but I am not sure how to do in C#.

*Edit: added a picture for the result with UTF32 Empty boxes with UTF32:

empty boxes with UTF32

1 Answers

This should do the job:

var input = new byte[5];

var encoding = Encoding.GetEncoding(Encoding.ASCII.EncodingName,
    new EncoderReplacementFallback(""),
    new DecoderReplacementFallback(""));
        
var converted = Encoding.Convert(Encoding.UTF8, encoding, input);

var output = encoding.GetString(converted);

This would remove all non-ascii chars with an empty string

Related