exception in c#.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moshesade
    New Member
    • Jan 2010
    • 3

    exception in c#.net

    i don't understand why the program throw exception?
    this method need to convert haxdecimal to simple string without 0x
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace hexdecimal
    {
        class Program
        {
            static void Main(string[] args)
            {
                string cleanInput;
                Console.WriteLine("insert hexdecimal number");
                string hd = Console.ReadLine();
                cleanInput = GetHexStringWithout0x(hd);
                Console.WriteLine("the values is {0}",cleanInput);       
            }
    
            private static string GetHexStringWithout0x(string input)
            {
                string answer;
                char[] hn = new char[input.Length];
                hn = input.ToCharArray(2, input.Length);
                answer = new string(hn);
                return answer;
            }       
        }
    }
    Last edited by tlhintoq; Jan 26 '10, 08:41 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Where does it throw the exception? (what line)
      What is the actual error?

      I do notice that line 24 might be a problem
      If your input.length is 30
      and you start at position 2, for a length of 30
      then you would have to have 32 elements to put into hn

      Comment

      • moshesade
        New Member
        • Jan 2010
        • 3

        #4
        if you help me

        how do i solve a problem?

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Don't try to grab the full length if you are starting someplace other than 0.
          You have to grab a length that is your full length minus your starting position.
          Code:
           private static string GetHexStringWithout0x(string input, int StartPos)
                  {
                      string answer;
                      char[] hn = new char[input.Length];
                      hn = input.ToCharArray(StartPos, input.Length - StartPos);
                      answer = new string(hn);
                      return answer;
                  }

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            If your goal is to just remove the 0x from the start of your string everytime:
            [code=c#]
            private static string GetHexStringWit hout0x(string input, int StartPos)
            {
            string retval = input;
            if (input.StartsWi th("0x", StringCompariso n.InvariantCult ureIgnoreCase)) retval = input.Substring (2);
            return answer;
            }
            [/code]

            Comment

            Working...