Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing output from standard io encoding?

I'm piping the output of an app into my .NET app.

The encoding is somewhat strange. Letters ÅÄÖ shows up as ├Ñ ├ñ ├Â

I have tried to convert back and forth from various different encodings w/o any success. Anyone know how the string should be converted correctly here?

e.g. the documentation for the app says the output is UTF8, so I've tried this:

byte[] encodedBytes = Encoding.UTF8.GetBytes(theOutput);
var res = Encoding.Default.GetString(encodedBytes);

Which goves the incorrect result.

edit: code:

var processStartInfo = new ProcessStartInfo
{
   CreateNoWindow = true,
   RedirectStandardOutput = true,
   RedirectStandardInput = true,
   UseShellExecute = false,
   Arguments = a,
   FileName = path + "\\phantomjs.exe"
};

var process = new Process
{
   StartInfo = processStartInfo,
   EnableRaisingEvents = true
};

//capturing output here
process.OutputDataReceived += 
   (sender, args) => outputBuilder.Append(args.Data);

process.Start();
process.BeginOutputReadLine();
process.WaitForExit(20000);
process.CancelOutputRead();
like image 737
Roger Johansson Avatar asked Dec 16 '22 07:12

Roger Johansson


1 Answers

Found the solution. You can set

   processStartInfo.StandardOutputEncoding = Encoding.UTF8;

This makes it output correctly.

like image 68
Roger Johansson Avatar answered Dec 18 '22 10:12

Roger Johansson