【问题标题】:Write bytes from textbox to file from basestream with C#使用 C# 将文本框中的字节写入基流中的文件
【发布时间】:2012-04-01 22:45:56
【问题描述】:

基本上我需要将文本框中的文本从 UTF-8 转换为 base16(我认为这就是十六进制的内容)并将其写入文件。

这不过是回话:

//Setup byte reader.
            FileStream fs = new FileStream(EditOpen.FileName, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            long length = fs.Length;
            //Read bytes to textBox1.
            br.BaseStream.Position = 0x00001844; //Min loading address.
            byte[] PT = br.ReadBytes(0x00000428); //Amount of bytes to load (1064 to be exact).

            //Print string PT to textBox1 after converting to UTF-8 and replace 0's with DOT's.
            textBox1.Text = System.Text.Encoding.UTF8.GetString(PT).Replace("\0", ".");
            fs.Close();

【问题讨论】:

    标签: c# utf-8 textbox binarywriter


    【解决方案1】:

    最简单的方法是使用用正确的encoding 创建的StreamWriter

      using(StreamWriter sw = 
                new System.IO.StreamWriter(fs, 
                    System.Text.UTF8Encoding))
      {
         sw.Write(textBox1.Text);
      }
    

    【讨论】:

    • 允许基地址吗?
    • 我相信底层 Stream 位置的变化也会影响 StreamWriter。我目前无法测试,但您应该试一试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多