阅读从Console.In二进制数据 [英] Read binary data from Console.In

查看:176
本文介绍了阅读从Console.In二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在C#读取从标准输入的二进制数据?

Is there any way to read binary data from stdin in C#?

在我的问题,我有一个启动并接收标准输入的二进制数据的程序。
基本上:
     C:> myImageReader< someImage.jpg

In my problem I have a program which is started and receives binary data on stdin. Basically: C:>myImageReader < someImage.jpg

和我想编写一个程序,如:

And I would like to write a program like:

static class Program
{
    static void Main()
    {
        Image img = new Bitmap(Console.In);
        ShowImage(img);
    }
}

不过Console.In是不是甲流,这是一个TextReader的。
(如果我试图读取到char [],该数据的TextReader间$ P $点,不要让我可以访问原始字节)。

However Console.In is not a Stream, it's a TextReader. (And if I attempt to read to char[], the TextReader interprets the data, not allowing me to get access to the raw bytes.)

任何人有关于如何访问实际二进制输入一个好主意?

Anyone got a good idea on how get access to the actual binary input?

干杯,
雷夫

推荐答案

要读取二进制,最好的办法是使用原始输入数据流 - 在这里展示stdin和stdout之间像回声

To read binary, the best approach is to use the raw input stream - here showing something like "echo" between stdin and stdout:

    using (Stream stdin = Console.OpenStandardInput())
    using (Stream stdout = Console.OpenStandardOutput())
    {
        byte[] buffer = new byte[2048];
        int bytes;
        while ((bytes = stdin.Read(buffer, 0, buffer.Length)) > 0) {
            stdout.Write(buffer, 0, bytes);
        }
    }

这篇关于阅读从Console.In二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆