checking of hextoascii code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trixxy
    New Member
    • Sep 2008
    • 26

    checking of hextoascii code

    Hi, i would like to know whether this code can be improved. because each time i convert extremely long strings, my exe hangs (does not respond).

    Code:
    private string HexString2Ascii(string HexString)
            {
                StringBuilder Sb = new StringBuilder();
                for (int i = 0; i <= hexTextBox.Text.Length - 2; i += 2)
                {
                    Sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(HexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber))));
                }
                return Sb.ToString();
            }
    
    private void hexToAsciiButton_Click_1(object sender, EventArgs e)
            {
                try
                {
                    asciiTextBox.Text = HexString2Ascii(hexTextBox.Text);
                }
    
                catch
                {
                    MessageBox.Show("Invalid Input", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    TIA
  • anijos
    New Member
    • Nov 2008
    • 52

    #2
    Are you getting index out of bound error?

    Comment

    • trixxy
      New Member
      • Sep 2008
      • 26

      #3
      no. i am able to convert long strings but not extremely long. the string that i want to convert contains about 2millions characters

      Comment

      • anijos
        New Member
        • Nov 2008
        • 52

        #4
        what is the error message u r getting?

        Comment

        • trixxy
          New Member
          • Sep 2008
          • 26

          #5
          there is no error message. the program just hangs. you need to open up the task manager and end task it to close it.

          Comment

          • anijos
            New Member
            • Nov 2008
            • 52

            #6
            Although the StringBuilder is a dynamic object that allows you to expand the number of characters in the string that it encapsulates.

            The capacity of a StringBuilder is the maximum number of characters the instance can store at any given time, and is greater than or equal to the length of the string representation of the value of the instance. The capacity can be increased or decreased with the Capacity property or EnsureCapacity method

            A StringBuilder can allocate more memory as needed to store characters when the value of an instance is enlarged, and the capacity is adjusted accordingly. The amount of memory allocated is implementation-specific, and ArgumentOutOfRa ngeException is thrown if the amount of memory required is greater than the maximum capacity.


            Try StringBuilder Sb = new StringBuilder(2 000000);

            AniJos

            Comment

            • trixxy
              New Member
              • Sep 2008
              • 26

              #7
              it still hangs. i tried lowering the value to 200,000 but it doesn't show the "ArgumentOutOfR angeException" error message

              Comment

              • anijos
                New Member
                • Nov 2008
                • 52

                #8
                The maximum size of all reference type (like a string) instances is limited
                by the CLR to 2GB, that means that a string can hold a maximum of ~1G
                characters.

                While it's possible to reach that limit when running on a 64 bit OS, you
                will never be able to create such large strings (or arrays) on a 32 bit OS.
                The reason is that you won't have that amount of "contiguous " address space
                available to create the backing store (a char array) for the string.
                The size of the largest contiguous memory space highly depends on how
                modules are mapped (see: Win32 and framework DLL's base addresses) into the process address space. Some modules are laid-out in such a way that the
                largest chunk becomes something like 950.000Kb, this before you even have
                created a single object.


                So try with smaller values till you find out what is the max capacity your system can afford.

                AniJos

                Comment

                • trixxy
                  New Member
                  • Sep 2008
                  • 26

                  #9
                  i found out something. it actually doesn't hang. it just takes a long time to process.
                  any way to shorten the process?

                  Comment

                  • balabaster
                    Recognized Expert Contributor
                    • Mar 2007
                    • 798

                    #10
                    Would it be possible to parse the string as if it were a stream? Dump it into some form of streamwriter and pass it into a streamreader and parse it on the fly?

                    Just a thought - it would mean you wouldn't need to read the string all in one go. I'm not sure if it's a valid way of doing this or if there's a more efficient way of doing it, just a thought...

                    Comment

                    • trixxy
                      New Member
                      • Sep 2008
                      • 26

                      #11
                      i have no idea on how to use a streamwriter

                      Comment

                      Working...