Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

HexCounter

0.00/5 (No votes)
17 Oct 2014 1  
HexCounter, Calculate the number HEX duplicate

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

Call all void in button with openFileDialog:

         // 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:

       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:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here