Hex редактор *.bin файлов - C#

Узнай цену своей работы

Формулировка задачи:

Добрый день ! Нужно сделать свой редактор bin файлов . Подскажите: Как правильно загрузить файл через openFileDialog. Считать в массив байт . И вывести в формате : номер строки , 16 байт через пробел в хекс виде , 16 байт через пробел в ASCII. Заранее спасибо всем откликнувшимся
 private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "bin files (*.bin)|*.bin|All files (*.*)|*.*";
 
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
            FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read);
            
            long nBytesRead = fs.Length;
//
}
как теперь массив разделить по 16 байт и вывести ?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
 
namespace Hex
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "bin files (*.bin)|*.bin|All files (*.*)|*.*";
 
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
            FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read);
            long nBytesRead = fs.Length;
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            string hex = BitConverter.ToString(bytes);
            hex = hex.Replace("-", " ");
            richTextBox1.Text = hex;
 
        }
    }
            }
        }

Решение задачи: «Hex редактор *.bin файлов»

textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
 
namespace Hex
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }
 
        private void Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "bin files (*.bin)|*.bin|All files (*.*)|*.*";
 
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
            System.IO.FileInfo file = new System.IO.FileInfo(openFileDialog.FileName);
            long size = file.Length;
            byte[] shc = new byte[size];
            using (FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open))
            {
                fs.Read(shc, 0, Convert.ToInt32(size));
            }
            dataGridView1.Rows.Clear();
            long kolStrok = size / 16;
            for (long i = 0; i < kolStrok; i++)
            {
                string id = (i * 16).ToString("X") + ":";
                id = id.PadLeft(7, '0');
                dataGridView1.Rows.Add(id);
            }
            long c = 0;
            for (int i = 0; i < kolStrok; i++)
            {
                for (byte b = 1; b < 17; b++)
                {
                    dataGridView1[b, i].Value = string.Format("{0:X2}", shc[c]); // Заполнение HEX поля 
                    c++;
                }
            }
            c = 0;
            for (int i = 0; i < kolStrok; i++)
            {
                for (byte b = 18; b < 34; b++)
                {
                    dataGridView1[b, i].Value = Convert.ToChar(shc[c]); // Заполнение ASCII поля
                    c++;
                }
            }
        }
            
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            int s = 20; // Ширина столбцов с 1 по 16
            int s2 = 12; // Ширина столбцов с 18 по 34
            dataGridView1.Columns[0].Width = 50;
 
            for (int i = 1; i < 17; i++)
            {
                dataGridView1.Columns[i].Width = s; // Присвоение ширины
                dataGridView1.Columns[i + 17].Width = s2; // Присвоение ширины
                ((DataGridViewTextBoxColumn)dataGridView1.Columns[i]).MaxInputLength = 2; // Ограничение на ввод 2 знаками
                ((DataGridViewTextBoxColumn)dataGridView1.Columns[i + 17]).MaxInputLength = 1; //Ограничение на ввод 1 знаком
            }
 
            dataGridView1.Columns[17].Width = 10;
 
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView1.AllowUserToResizeRows = false;
 
        }
 
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
 
        }
 
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
          //  dataGridView1[b, i].Value = string.Format("{0:X2}", shc[c]);
        }
    }
    }

Оцени полезность:

15   голосов , оценка 3.733 из 5