简体   繁体   中英

C# How to convert ASCII Ledger-CLI output to UTF-8 input for further processing

I have to create a C# core command-line app to replace and extend a working python script.

Basically, the first step is to capture the output of the Ledger command-line accounting program and process it further on. I believe that the output is ASCII: the is displayed as Γé¼ .

The python script converts the output to UTF-8 with a simple output.decode("utf-8") which turns all Γé¼ nicely into

My C# program captures the output in a variable and I have been searching around the net for a simple recipe to convert it to UTF-8 (ie achieve the python functionality) in Dotnet core, but I haven't found a working one yet.

This is how I capture the output:

public string Fetch(string naam)
{
    using (Process p = new Process())
    {
         p.StartInfo.FileName = @"c:\ProgramData\chocolatey\bin\ledger.exe";
         p.StartInfo.Arguments = $"-f c:\\Users\\guivho\\ledger\\boekhouding.ledger -U csv t:{naam} --sort date";
         p.StartInfo.UseShellExecute = false;
         p.StartInfo.RedirectStandardOutput = true;
         p.Start();
         string ouput = p.StandardOutput.ReadToEnd();
         p.WaitForExit();
         return ouput;
    }
}

Please advise and TIA for any help or suggestions.

The problem was not in the output of the ledger program. Solved the issue by having Console.OutputEncoding = System.Text.Encoding.UTF8; as the first instruction of the Main function of the program before it ever called Ledger (which was completely unrelated to the problem):

static void Main(string[] args)
    {
        Console.OutputEncoding = System.Text.Encoding.UTF8;
        ...
    }

Thanks for all help, and a Happy Healthy New Year to you all!

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