-
Notifications
You must be signed in to change notification settings - Fork 3
/
HexForm.cs
162 lines (146 loc) · 6.4 KB
/
HexForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SAD806x
{
public partial class HexForm : Form
{
private DataTable dtBytes = null;
public HexForm(string[] binBytes, SortedList slOrderedBanks)
{
DataColumn dcDc = null;
int bankIndex = -1;
int bankNum = -1;
int bankStartAddress = -1;
int bankEndAddress = -1;
bool bankMode = false;
dtBytes = new DataTable();
dcDc = new DataColumn();
dcDc.ColumnName = "Offset";
dcDc.DataType = typeof(string);
dtBytes.Columns.Add(dcDc);
if (slOrderedBanks.Count > 0)
{
dcDc = new DataColumn();
dcDc.ColumnName = "Bank Offset";
dcDc.DataType = typeof(string);
dtBytes.Columns.Add(dcDc);
bankMode = true;
bankIndex = -1;
}
for (int iCol = 0; iCol < 16; iCol++)
{
dcDc = new DataColumn();
dcDc.Caption = string.Format("{0:X2}", iCol);
dcDc.ColumnName = dcDc.Caption;
dcDc.DataType = typeof(string);
dtBytes.Columns.Add(dcDc);
}
dcDc = new DataColumn();
dcDc.ColumnName = "Ascii";
dcDc.DataType = typeof(string);
dtBytes.Columns.Add(dcDc);
dcDc = null;
for (int iPos = 0; iPos < binBytes.Length; iPos+=16)
{
string[] hexLine = null;
if (bankMode) hexLine = new string[19];
else hexLine = new string[18];
string asciiLine = string.Empty;
hexLine[0] = string.Format("{0:X5}", iPos);
if (bankMode)
{
while (iPos > bankEndAddress)
{
bankIndex++;
if (bankIndex > slOrderedBanks.Count - 1)
{
bankStartAddress = binBytes.Length;
bankEndAddress = binBytes.Length;
break;
}
else
{
bankNum = ((int[])slOrderedBanks.GetByIndex(bankIndex))[0];
bankStartAddress = ((int[])slOrderedBanks.GetByIndex(bankIndex))[1];
bankEndAddress = ((int[])slOrderedBanks.GetByIndex(bankIndex))[2];
}
}
hexLine[1] = string.Empty;
if (iPos >= bankStartAddress && iPos <= bankEndAddress)
{
hexLine[1] = string.Format("{0,1} {1:X4}", bankNum, iPos - bankStartAddress + SADDef.EecBankStartAddress);
}
}
for (int subPos = 0; subPos < 16 && subPos + iPos < binBytes.Length; subPos++)
{
if (bankMode) hexLine[subPos + 2] = binBytes[subPos + iPos].ToUpper();
else hexLine[subPos + 1] = binBytes[subPos + iPos].ToUpper();
asciiLine += Convert.ToChar(Convert.ToByte(binBytes[subPos + iPos], 16));
}
hexLine[hexLine.Length - 1] = asciiLine;
dtBytes.LoadDataRow(hexLine, false);
}
try { Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); }
catch { }
InitializeComponent();
}
private void HexForm_Load(object sender, EventArgs e)
{
hexDataGridView.DataSource = dtBytes;
for (int iCol = 0; iCol < hexDataGridView.Columns.Count; iCol++)
{
if (iCol == 0)
{
hexDataGridView.Columns[iCol].Width = 45;
hexDataGridView.Columns[iCol].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
hexDataGridView.Columns[iCol].DefaultCellStyle.BackColor = hexDataGridView.ColumnHeadersDefaultCellStyle.BackColor;
}
else if (iCol == 1 && hexDataGridView.Columns.Count == 19)
{
hexDataGridView.Columns[iCol].Width = 45;
hexDataGridView.Columns[iCol].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
hexDataGridView.Columns[iCol].DefaultCellStyle.BackColor = hexDataGridView.ColumnHeadersDefaultCellStyle.BackColor;
}
else if (iCol == hexDataGridView.Columns.Count - 1)
{
hexDataGridView.Columns[iCol].Width = 150;
hexDataGridView.Columns[iCol].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
hexDataGridView.Columns[iCol].DefaultCellStyle.Font = new Font(FontFamily.GenericMonospace, hexDataGridView.Font.Size);
hexDataGridView.Columns[iCol].DefaultCellStyle.BackColor = hexDataGridView.ColumnHeadersDefaultCellStyle.BackColor;
}
else hexDataGridView.Columns[iCol].Width = 25;
}
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
StringBuilder sB = new StringBuilder(string.Empty);
SortedList slCells = null;
if (hexDataGridView.SelectedCells != null)
{
slCells = new SortedList();
foreach (DataGridViewCell cCell in hexDataGridView.SelectedCells)
{
switch (hexDataGridView.Columns[cCell.ColumnIndex].Name.ToUpper())
{
case "OFFSET":
case "BANK OFFSET":
case "ASCII":
break;
default:
slCells.Add(cCell.ColumnIndex + cCell.RowIndex * hexDataGridView.RowCount, cCell.Value);
break;
}
}
foreach (string cellText in slCells.Values) sB.Append(cellText);
slCells = null;
}
Clipboard.SetText(sB.ToString());
sB = null;
}
}
}