D
D
Dmitry2022-02-23 11:26:43
C++ / C#
Dmitry, 2022-02-23 11:26:43

Why does it take a long time to convert to binary code, with a larger amount of text (112 KB file)?

private void button2_Click(object sender, EventArgs e)
        {
            string path = "";
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                path = ofd.FileName;
            }
            StreamReader reader = new StreamReader(path);
            byte[] buf = Encoding.UTF8.GetBytes(reader.ReadToEnd());
            StringBuilder sb = new StringBuilder(buf.Length * 8);
            foreach (byte b in buf)
            {
                sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
            }
            string binaryStr = sb.ToString();

            textBox1.Text = binaryStr;
            TextWriter tw = new StreamWriter(@"W:\du.txt");
            tw.WriteLine(binaryStr);
            tw.Close();
        }

Because of what it can work slowly and how can it be fixed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2022-02-23
@roflanPominki

Well, connect the profiler and see what you are doing for a long time.
Most likely - Convert.ToString works for a long time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question