0

I have this string that contains the character "á" and "ã" and it doesn't display them. The "ã" is being displayed as "Æ" and the "á" doesn't appear. I've gone through many questions here on SO but none of them solved my issue, such as this one: How can I transform string to UTF-8 in C#?, so it might not do much to redirect me to another question unless it has a really different answer.

Well, the code I'm currently using is that of the question I linked back there, which is:

byte[] bytes = Encoding.Default.GetBytes(myString);
myString = Encoding.UTF8.GetString(bytes);

With this, the special characters both show up as question marks (�).

And if I change the UTF8 to UTF32, it shows the entire string with those question marks.

I've also tried this:

Encoding enc = Encoding.GetEncoding("ISO-8859-1");
string original = myString;
byte[] iso_8859_1 = enc.GetBytes(original);
string roundTripped = enc.GetString(iso_8859_1);
Debug.Assert(original == roundTripped);

Which gives me the same output as the initial one, before trying to change the encoding (so it basically does nothing to my string).

I've checked the config file of the app I'm using it on, and the encoding is set to UTF-8, so that's probably not the issue here either.

Does anyone know what might be wrong, or some other way to "decode" my string and show the correct output?

Also, I've tried the answer from this question: Encoding issue when handling a string that contains "question mark" (�) and if I actually use the word "ESPAÑOL" it shows up fine, but if I put in my string it doesn't. The only "special" characters in it are those two I mentioned, unless the spaces and line changes make a difference as well (but I have no idea).

If any further information would be helpful, please let me know, I've been trying to fix this for hours now.

-EDIT-

This is how I get myString:

Process proc = new Process();
proc.StartInfo.StandardOutputEncoding = Encoding.Unicode;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "start /c cscript.exe /U " + Path.Combine(Environment.SystemDirectory, "slmgr.vbs") + " /xpr";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
string q = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
user692942
  • 16,398
  • 7
  • 76
  • 175
S. M.
  • 227
  • 1
  • 12

1 Answers1

0

The conversion from string to bytes converts to ASCII. I use Unicode for both conversions.

byte[] bytes = Encoding.Unicode.GetBytes(myString);
myString = Encoding.Unicode.GetString(bytes);
Tom Henn
  • 125
  • 6
  • This gives me the initial output, "ã" as Æ and "á" as empty. – S. M. Nov 07 '18 at 11:15
  • I'm sure you mean to show that the transformations are reversible. If it's not clear to others, the two together result in no transformation. – Tom Blodget Nov 07 '18 at 12:16