Capturing output from standard io encoding?
Asked Answered
S

1

5

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();
Symmetrize answered 6/8, 2013 at 13:10 Comment(5)
How are you obtaining this output to start with? Please give more context. Converting a string to bytes and back using a different encoding is almost always a bad idea.Billon
@Mgetz: It may well be - but I'm waiting to find out. (Just making the other app write to a file would be a good way of controlling things...)Billon
@JonSkeet yes, if I pipe the same command to a file, the output looks correct... e.g. phantomjs.exe myscript.js > foo.txtSymmetrize
@RogerAlsing Appears correct HOW? What encoding does it have the file? If you try to open it with an "advanced" editor (Notepad++ for example), is the file recognized as ANSI or UTF-8?Liquidize
And I see you are from Sweden... Do you know your codepage? You should be 1252 for Windows and 865 for DOS (could you try to launch chcp in a command prompt?)Liquidize
S
15

Found the solution. You can set

   processStartInfo.StandardOutputEncoding = Encoding.UTF8;

This makes it output correctly.

Symmetrize answered 6/8, 2013 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.