Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remarks for StandardInputEncoding #6849

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

imba-tjd
Copy link
Contributor

@imba-tjd imba-tjd commented Jun 28, 2021

Do reword my content if necessary.

Reason

This is a C Program that reads by UTF16:

#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <wchar.h>

int main() {
    _setmode(_fileno(stdin), _O_U16TEXT);
    _setmode(_fileno(stdout), _O_U16TEXT);

    wchar_t input[10] = {};
    fgetws(input, 10, stdin);
    wprintf(L"%X\n", input[0]);
    fputws(input, stdout);
}

When executing it from Console, it works normally. For example type asdf:

asdf # Type
61
asdf

But when executing it from C# with Encoding.Unicode, it won't work normally:

using System;
using System.Text;
using System.Diagnostics;

class Program {
    static void Main() {
        ProcessStartInfo psi = new() {
            FileName = "tr.exe",
            UseShellExecute = false,
            RedirectStandardInput = true,
            StandardInputEncoding = Encoding.Unicode,
            // StandardInputEncoding = new UnicodeEncoding(false, false),
            RedirectStandardOutput = true,
            StandardOutputEncoding = Encoding.Unicode
        };
        var p = Process.Start(psi);
        p.StandardInput.WriteLine("asdf");
        p.WaitForExit();
        string s = p.StandardOutput.ReadToEnd();
        Console.WriteLine(s);
    }
}

The output:

FEFF
?asdf

FEFF is UTF16 BE BOM. I think it's because C is LE, the actual bytes are FF, FE, UTF16 LE, but when outputing the FE is interpreted as high part.

The correct way is to use new UnicodeEncoding(false, false).

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@opbld31
Copy link

opbld31 commented Jun 28, 2021

Docs Build status updates of commit d9c97fd:

✅ Validation status: passed

File Status Preview URL Details
xml/System.Diagnostics/ProcessStartInfo.xml ✅Succeeded View

For more details, please refer to the build report.

Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report.

For any questions, please:

@ghost
Copy link

ghost commented Jun 28, 2021

Tagging subscribers to this area: @carlossanlop
See info in area-owners.md if you want to be subscribed.

Issue Details

Do reword my content if necessary.

Reason

This is a C Program that reads by UTF16:

#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <wchar.h>

int main() {
    _setmode(_fileno(stdin), _O_U16TEXT);
    _setmode(_fileno(stdout), _O_U16TEXT);

    wchar_t input[10] = {};
    fgetws(input, 10, stdin);
    wprintf(L"%X\n", input[0]);
    fputws(input, stdout);
}

When executing it from Console, it works normally. For example type asdf:

asdf # Type
61
asdf

But when executing it from C# with Encoding.Unicode, it won't work normally:

using System;
using System.Text;
using System.Diagnostics;

class Program {
    static void Main() {
        ProcessStartInfo psi = new() {
            FileName = "tr.exe",
            UseShellExecute = false,
            RedirectStandardInput = true,
            StandardInputEncoding = Encoding.Unicode,
            // StandardInputEncoding = new UnicodeEncoding(false, false),
            RedirectStandardOutput = true,
            StandardOutputEncoding = Encoding.Unicode
        };
        var p = Process.Start(psi);
        p.StandardInput.WriteLine("asdf");
        p.WaitForExit();
        string s = p.StandardOutput.ReadToEnd();
        Console.WriteLine(s);
    }
}

The output:

FEFF
?asdf

The FEFF is UTF-16 LE BOM.

The correct way is to use new UnicodeEncoding(false, false).

Author: imba-tjd
Assignees: -
Labels:

area-System.Diagnostics.Process

Milestone: -

@eiriktsarpalis eiriktsarpalis removed their request for review June 28, 2021 12:35
@krwq
Copy link
Member

krwq commented Jun 29, 2021

cc: @adamsitnik @carlossanlop @jozkee

@opbld30
Copy link

opbld30 commented Jun 29, 2021

Docs Build status updates of commit 25b152e:

✅ Validation status: passed

File Status Preview URL Details
xml/System.Diagnostics/ProcessStartInfo.xml ✅Succeeded View

For more details, please refer to the build report.

Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report.

For any questions, please:

@opbld31
Copy link

opbld31 commented Jun 29, 2021

Docs Build status updates of commit 71cc355:

✅ Validation status: passed

File Status Preview URL Details
xml/System.Diagnostics/ProcessStartInfo.xml ✅Succeeded View

For more details, please refer to the build report.

Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report.

For any questions, please:

@opbld30
Copy link

opbld30 commented Jun 29, 2021

Docs Build status updates of commit 71cc355:

✅ Validation status: passed

File Status Preview URL Details
xml/System.Diagnostics/ProcessStartInfo.xml ✅Succeeded View

For more details, please refer to the build report.

Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report.

For any questions, please:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants