简体   繁体   中英

c#: How do i remove "broken" chars from a string? �

My application runs cmd commands and saves the output in a string. Whenever there is a german umlaut in the output, the content looks for example like this:

Id Name                 MainWindowTitle     
14892 TextInputHost        Microsoft Text Input Application                                                
6712 VirtualBox           Oracle VM VirtualBox Manager                                                    
2124 VirtualBoxVM         Win10_2 [wird ausgef�hrt] - Oracle VM VirtualBox   

How do i replace/remove the broken chars? like the �, because it doesnt let me encode the string in base64 without information loss.

string activeProcesses = SysHelper.GetForegroundProcesses(); //runs a cmd-process and returns the string with the broken character above

Ok, so the problem was, that the "broken" char, prevented me from encoding the string to base64. If I did, the base64 string was broken and when decoding it, I received only alien signs.

I fixed it, by using

string processStrClean = Encoding.Default.GetString(Convert.FromBase64String(activeProcesses));

instead of

string processStrClean = Encoding.UTF8.GetString(Convert.FromBase64String(activeProcesses));

So, actually, it was the UTF8 encoding, which broke it. After using the Default encoding, I could encode the string into a base64 string and later decode it, without loss of information.

And always remember, kids, real heroes answer their own questions at stack overflow, once they find out the solution to their problem. Maybe someone finds this thread in 5 years and you save his day.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM