FileStream Write() 方法仅写入未知符号

发布于 2024-12-27 19:03:44 字数 346 浏览 0 评论 0原文

这是我的代码,它只在 fileout.txt 中写入这些符号。出了什么问题以及如何修复它?

byte[] bArray = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        FileStream file_out = new FileStream(@"C:\fileout.txt",
            FileMode.Create);

        file_out.Write(bArray, 2, 4);

        file_out.Close();

抱歉,此处未显示符号,但我不知道为什么。这个符号类似于上、小和其他一些符号。

this is my code and it writes only this symbols in fileout.txt. whats wrong and how to fix it?

byte[] bArray = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        FileStream file_out = new FileStream(@"C:\fileout.txt",
            FileMode.Create);

        file_out.Write(bArray, 2, 4);

        file_out.Close();

sorry, symbols not shown here but why i dont know. this simbols ar something like upside and small r an some other symbols.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心奴独伤 2025-01-03 19:03:44

您的输出是实际的字节,而不是向讲英语的人(以及许多其他语言,但不是全部)表示它们的字符。

您要编写的字符是一些主要具有历史意义的控件,尽管美联社仍然使用 ANPA-1312 并且它使用它们:

文本开始位于消息的实际文本之前。 (0x01,您跳过的是标题的开始。END

OF TEXT 文本结束。此外,Ctrl+C 可以结束正在运行的 DOS 程序。END

OF TRANSMISSION 再见。用作注销字符

。INQUIRY你还在吗?

数组中要打印的还包括:

NULL(空字符,不是 C# 中的 null 含义):在某些格式中用于表示字符串的结尾,“end”有意义的文本”等。

标题开始:上面提到过。

确认是的,我在这里。也用于 TCP/IP 握手。

贝尔 仍然有打印机,如果你向它们发送此信息,它们就会发出声音。

退格键打印 u,然后退格,然后 ¡ 曾经是您编写 ü 的方式,请记住,如果 Unicode 看起来比 ASCII 更复杂,那么实际上它更简单。只需有一个 ü 即可。另外,back 和 X 和 back 和 X 和 back 和 X 是一个旧的“删除”

字符,您可能会在现代代码中使用这些字符中的第一个字符 ! “制表符”的级别不是很低。当然,它本身看起来并不多。

您可能想要输出字符 '0''1' 等。

您可以:

手动对它们进行编码。 '0' 为 0x30,'1' 为 0x31。这是您应该知道如何做但 99% 的情况下您自己实际上不做的事情之一。

使用文本编写器。这采用不同类型的值并(可选地使用定义的格式规则)输出表示它们的文本。因此,

using(FileStream file_out = new FileStream(@"C:\fileout.txt", FileMode.Create)
using(TextWriter tw = new StreamWriter(file_out))
  for(int i = 2; i != 6; ++i)
    tw.Write(bArray[i]);

这会将字节转换为人类可读的等价物,并且非常有效。这与输出 0x32、0x33、0x34、0x35 相同,在记事本中打开时看起来像“2345”。

如果您从其他地方接收字节(例如从某个地方创建或获取图像流)或执行非常低级别的操作,那么您最想直接写入字节。

Your output is the actual bytes, rather than the characters that represent them to English-speaking humans (and many other languages, but not all).

The characters you would have written are some controls of mostly historical interest, though ANPA-1312 is still used by Associated Press and it uses them:

START OF TEXT Precedes the actual text of the message. (0x01, that you skipped is the start of a heading.

END OF TEXT End of the text. Also, Ctrl+C to end a running DOS program is this control.

END OF TRANSMISSION Goodbye. Used as a log-out character.

ENQUIRY You still there?

Those in your array to print also include:

NULL (null char, not what null means in C#): Used in some formats to represent the end of a string, "end of meaningful text", etc.

START OF HEADER: Mentioned above.

ACKNOWLEDGE Yes, I'm here. Also used in TCP/IP hand-shaking.

BELL There are still printers out there that will go ding if you send them this.

BACKSPACE Printing u, then backspace then ¨ was once how you would write ü. Remember that if Unicode seems more complicated than ASCII. Actually it's simpler to just have a ü. Also back and X and back and X and back and X was an old "delete"!

CHARACTER TAB the first of these characters that you are ever likely to use in modern code that isn't very low level. The "tab" character. Of course, it doesn't look like much on its own.

What you perhaps wanted was to output the characters, '0', '1', and so on.

You could:

Manually encode them. '0' is 0x30, '1' is 0x31. This is one of those things where you should know how to do it, but not actually do it yourself 99% of the time.

Use a textwriter. This takes values of different types and (optionally with defined formatting rules) outputs the text that represents them. Hence

using(FileStream file_out = new FileStream(@"C:\fileout.txt", FileMode.Create)
using(TextWriter tw = new StreamWriter(file_out))
  for(int i = 2; i != 6; ++i)
    tw.Write(bArray[i]);

This will convert the bytes to their human-readable equivalent as it goes, and pretty efficiently. It's the same as if you'd output 0x32, 0x33, 0x34, 0x35, which will look like "2345" when opened in notepad.

You will mostly want to write bytes directly if you're receiving them from somewhere else (such as having created or obtained an image stream from somewhere) or doing something very low-level.

你是年少的欢喜 2025-01-03 19:03:44

是的,您正在写出字节 2、3、4、5。这些都不是我所知道的任何编码中的可打印文本。

我不清楚你的期望是什么,或者为什么你期望它最终成为可打印的文本 - 但如果你想写文本(而不是二进制) )数据,您应该使用某种形式的TextWriter,例如StreamWriter

Yes, you're writing out the bytes 2, 3, 4, 5. Those aren't printable text in any encoding I'm aware of.

It's not clear to me what you expected, or why you'd have expected it to end up as printable text - but if you want to write text (as opposed to binary) data you should use a TextWriter of some form, e.g. a StreamWriter.

最近可好 2025-01-03 19:03:44

您应该使用类似的内容

    public static void Main()
    {
        byte[] bArray = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        using (StreamWriter outfile =
        new StreamWriter(@"C:\\fileout.txt"))
        {
            foreach (var b in bArray)
            {
                outfile.Write(b);
            }
        }

        Console.ReadLine();
    }

这将生成一个文本文件,其中包含

0123456789

You should use something like

    public static void Main()
    {
        byte[] bArray = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        using (StreamWriter outfile =
        new StreamWriter(@"C:\\fileout.txt"))
        {
            foreach (var b in bArray)
            {
                outfile.Write(b);
            }
        }

        Console.ReadLine();
    }

This will result in a text file that contains

0123456789

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文