Click here to Skip to main content
15,919,500 members
Articles / Desktop Programming / MFC
Tip/Trick

HexCounter

Rate me:
Please Sign up or sign in to vote.
4.78/5 (6 votes)
17 Oct 2014MIT 8.8K   147   7  
HexCounter, Calculate the number HEX duplicate

Image 1

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

Call all void in button with openFileDialog:

C#
// openFileDialog1=ofd
ofd.Title = "Hex";
//filter file
   ofd.Filter = "Text Normal|*.txt|Text INF|*.inf";
   if (ofd.ShowDialog() != DialogResult.Cancel)
   {
    // start timer for check speed program option
       Benchmark.Start();
    //call void read file
       Load_file(ofd.FileName, listBox1, true);  //set address
       Counter_hex(listBox1, listBox2);    //counter hex
       //add_index(listBox1);             // add list index option
       //button2.Enabled = false;         // control button off just one click
       Benchmark.End();                   //end timer
       double seconds = Benchmark.GetSeconds(); //calculate time run void
       MessageBox.Show(seconds.ToString());
   }

Calculate hex and add to list:

C#
public void Counter_hex(ListBox list1, ListBox list2)
 {

     _temp = list1.Items[0].ToString();      //get first hex
     int _temp2 = 1;                         //base start

     for (int h = 1; h <= list1.Items.Count - 1; h++)    // star main loop
     {
         for (int i = 0; i <= list1.Items.Count - 1; i++) //find duplicate hex
         {
   //counter hex
             if (_temp == list1.Items[i].ToString())
             {
                 _temp2++;
             }
         }
   // add to list with position (if sort=true)
         list2.Items.Add(_temp + "=" + ((_temp2 - h)).ToString() +
         "|" + _temp2.ToString() + "-" + h.ToString());
         if (_temp2 < list1.Items.Count)
         {
             int b = _temp2;
             _temp = list1.Items[b].ToString();
         }
      //jumper to other hex
         h = _temp2 - 1;
     }
 }

Load File with FileStream and BinaryReader:

I use ListBox for sample, but you can use:

C#
List<string> list = new List<string>; // it is more effective 
</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++)
            {
      //3 way i tested for speed reading with array[]
      //add.Items.Add(cp[i].ToString() + cp[i+1].ToString());  //(char)
      // add.Items.Add(Convert.ToChar(magic2.Read()).ToString());
      //add.Items.Add(Convert.ToChar(magic2.Read()).ToString() + Convert.ToChar(magic2.PeekChar()));
       
         add.Items.Add((char)(magic2.Read()) + Convert.ToChar(magic2.PeekChar()).ToString());
                 
              i++;  
              }
     // sorted text option 
            if (sort==true)
            {
                add.Sorted = true;
            }
        }

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Program Manager none
Iran (Islamic Republic of) Iran (Islamic Republic of)
Programming is my Job!

unity
C#
php
Sql
Joomla
Jave
VB
Maxscript
CSS

Comments and Discussions

 
-- There are no messages in this forum --