简体   繁体   English

写从文本的字节从basestream文件用C#

[英]Write bytes from textbox to file from basestream with C#

Basically I need to convert text in a textbox from UTF-8 to base16 (I think that is what hex is in) and write it to a file. 基本上,我需要将文本框中的文本从UTF-8转换为base16(我认为这是十六进制的内容)并将其写入文件。

This but back words: 这回话:

//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();

The easiest way is to use a StreamWriter created with the correct encoding 最简单的方法是使用一个StreamWriter的与正确的创建编码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM