![Image 1](/KB/cs/828748/counter.jpg)
Introduction
Hex Counter code to calculate the number of characters duplicate.
Background
The program for the identification of duplicate code (2 bytes) and build a compression algorithm written.
It can quickly read hex numbers and count the number of duplicates. It may be useful to you.
Using the Code
![Image 2](data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==)
Call all void
in button with openFileDialog
:
ofd.Title = "Hex";
ofd.Filter = "Text Normal|*.txt|Text INF|*.inf";
if (ofd.ShowDialog() != DialogResult.Cancel)
{
Benchmark.Start();
Load_file(ofd.FileName, listBox1, true);
Counter_hex(listBox1, listBox2);
Benchmark.End();
double seconds = Benchmark.GetSeconds();
MessageBox.Show(seconds.ToString());
}
Calculate hex and add to list:
public void Counter_hex(ListBox list1, ListBox list2)
{
_temp = list1.Items[0].ToString();
int _temp2 = 1;
for (int h = 1; h <= list1.Items.Count - 1; h++)
{
for (int i = 0; i <= list1.Items.Count - 1; i++)
{
if (_temp == list1.Items[i].ToString())
{
_temp2++;
}
}
list2.Items.Add(_temp + "=" + ((_temp2 - h)).ToString() +
"|" + _temp2.ToString() + "-" + h.ToString());
if (_temp2 < list1.Items.Count)
{
int b = _temp2;
_temp = list1.Items[b].ToString();
}
h = _temp2 - 1;
}
}
Load File with FileStream
and BinaryReader
:
I use ListBox
for sample, but you can use:
List<string> list = new List<string>;
</string>
private void Load_file(string Address_file,ListBox add,bool sort)
{
FileStream magic = new FileStream(Address_file,FileMode.Open,FileAccess.Read);
BinaryReader magic2 = new BinaryReader(magic);
int _length = (int) magic.Length;
for (int i = 0; i <= _length - 2; i++)
{
add.Items.Add((char)(magic2.Read()) + Convert.ToChar(magic2.PeekChar()).ToString());
i++;
}
if (sort==true)
{
add.Sorted = true;
}
}