Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. convert.ToInt32

convert.ToInt32

Scheduled Pinned Locked Moved C#
helpquestion
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yueru
    wrote on last edited by
    #1

    line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
    string Duration = line.Substring(64, 9).Replace("#","[#]");
    int s = Convert.ToInt32(Duration.Substring(1, 2));
    int s = Convert.ToInt32(Duration.Substring(1, 2));
    int y = Convert.ToInt32(Duration.Substring(3, 2));
    int l = Convert.ToInt32(Duration.Substring(6, 2));
    int NDura = s + y + l;

    ---->>>> Error System.FormatException: Input string was not in a correct format. So what should I do with this ?

    D S T 3 Replies Last reply
    0
    • Y yueru

      line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
      string Duration = line.Substring(64, 9).Replace("#","[#]");
      int s = Convert.ToInt32(Duration.Substring(1, 2));
      int s = Convert.ToInt32(Duration.Substring(1, 2));
      int y = Convert.ToInt32(Duration.Substring(3, 2));
      int l = Convert.ToInt32(Duration.Substring(6, 2));
      int NDura = s + y + l;

      ---->>>> Error System.FormatException: Input string was not in a correct format. So what should I do with this ?

      D Offline
      D Offline
      dotnetmember
      wrote on last edited by
      #2

      This is very silly mistake which u can trace it out.

      yueru wrote:

      int s = Convert.ToInt32(Duration.Substring(1, 2)); int s = Convert.ToInt32(Duration.Substring(1, 2)); int y = Convert.ToInt32(Duration.Substring(3, 2)); int l = Convert.ToInt32(Duration.Substring(6, 2));

      In above four lines, check wat is the value of (Duration.Substring()); it may be null or may not be in format to convert to integer.

      B 1 Reply Last reply
      0
      • Y yueru

        line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
        string Duration = line.Substring(64, 9).Replace("#","[#]");
        int s = Convert.ToInt32(Duration.Substring(1, 2));
        int s = Convert.ToInt32(Duration.Substring(1, 2));
        int y = Convert.ToInt32(Duration.Substring(3, 2));
        int l = Convert.ToInt32(Duration.Substring(6, 2));
        int NDura = s + y + l;

        ---->>>> Error System.FormatException: Input string was not in a correct format. So what should I do with this ?

        S Offline
        S Offline
        Spunky Coder
        wrote on last edited by
        #3

        yueru wrote:

        int s = Convert.ToInt32(Duration.Substring(1, 2)); int s = Convert.ToInt32(Duration.Substring(1, 2));

        I don't understand how the code has got compiled! :doh:

        yueru wrote:

        So what should I do with this ?

        Submit it to Microsoft critical bugs department :-D

        yueru wrote:

        Error System.FormatException: Input string was not in a correct format.

        Finally what is the value you are expecting in the

        Duration

        string ? Do the debug and verify if it has the desired value or not...

        "Don't worry if it doesn't work right. If everything did, you'd be out of a job." (Mosher's Law of Software Engineering)

        1 Reply Last reply
        0
        • Y yueru

          line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
          string Duration = line.Substring(64, 9).Replace("#","[#]");
          int s = Convert.ToInt32(Duration.Substring(1, 2));
          int s = Convert.ToInt32(Duration.Substring(1, 2));
          int y = Convert.ToInt32(Duration.Substring(3, 2));
          int l = Convert.ToInt32(Duration.Substring(6, 2));
          int NDura = s + y + l;

          ---->>>> Error System.FormatException: Input string was not in a correct format. So what should I do with this ?

          T Offline
          T Offline
          tolw
          wrote on last edited by
          #4

          The quick answer:

          int s = Convert.ToInt32(Duration.Substring(1, 1));

          Why? Try changing your code to this:

          String line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
          string Duration = line.Substring(64, 9).Replace("#", "[#]");
          string a = Duration.Substring(1, 2);
          string b = Duration.Substring(3, 2);
          string c = Duration.Substring(6, 2);
          int s = Convert.ToInt32(a);
          int y = Convert.ToInt32(b);
          int l = Convert.ToInt32(c);
          int NDura = s + y + l;

          You will discover that duration equals " 0:01'22 " and so the string you are trying to parse is "0:" - thats why you keep getting the error. Also this line:

          string Duration = line.Substring(64, 9).Replace("#", "[#]");

          Is kind of wierd as the Replace has no effect - as I mentioned Duration == " 0:01'22 ". No "#" there...

          Y 1 Reply Last reply
          0
          • D dotnetmember

            This is very silly mistake which u can trace it out.

            yueru wrote:

            int s = Convert.ToInt32(Duration.Substring(1, 2)); int s = Convert.ToInt32(Duration.Substring(1, 2)); int y = Convert.ToInt32(Duration.Substring(3, 2)); int l = Convert.ToInt32(Duration.Substring(6, 2));

            In above four lines, check wat is the value of (Duration.Substring()); it may be null or may not be in format to convert to integer.

            B Offline
            B Offline
            battulga_dddddd
            wrote on last edited by
            #5

            Int32.Parse(String)

            battulga

            1 Reply Last reply
            0
            • T tolw

              The quick answer:

              int s = Convert.ToInt32(Duration.Substring(1, 1));

              Why? Try changing your code to this:

              String line = "==XXXT08746=05/01/09 15:13 112 4 IT 0:01'22 0 #2 ";
              string Duration = line.Substring(64, 9).Replace("#", "[#]");
              string a = Duration.Substring(1, 2);
              string b = Duration.Substring(3, 2);
              string c = Duration.Substring(6, 2);
              int s = Convert.ToInt32(a);
              int y = Convert.ToInt32(b);
              int l = Convert.ToInt32(c);
              int NDura = s + y + l;

              You will discover that duration equals " 0:01'22 " and so the string you are trying to parse is "0:" - thats why you keep getting the error. Also this line:

              string Duration = line.Substring(64, 9).Replace("#", "[#]");

              Is kind of wierd as the Replace has no effect - as I mentioned Duration == " 0:01'22 ". No "#" there...

              Y Offline
              Y Offline
              yueru
              wrote on last edited by
              #6

              Thank you I 've got it even if I've made alot of mistake like u said now is my code

              string Duration = line.Replace("'", "[']");
              string LineOO = line.Replace("#", "[#]");
              string Date = line.Substring(12, 8);
              if (line.Substring(11, 1) == "=")
              {
              string Time = line.Substring(21, 5);
              string Linein = line.Substring(27, 10);
              string Lineout = LineOO.Substring(38, 1);

                              if ((string.Compare(Lineout, "0") > 0 || string.Compare(Lineout, "0") == 0) && (string.Compare(Lineout, "9") < 0 || string.Compare(Lineout, "9") == 0))
                              {
                                 
                                  //int g = line.Length;
                                  //string Number = line.Substring(40, g - 40);
                                  string Number = line.Substring(40, 20);
                                  string Status = line.Substring(61, 3);
                                string NDuration = Duration.Substring(64, 10);
                                  
              
                                 // string a = Duration.Substring(1, 2);
                                         string b = NDuration.Substring(3, 2);
                                  string c = NDuration.Substring(6, 2);
                                 // int s = Convert.ToInt32(a);
                                  int y = Convert.ToInt32(b);
                                  int l = Convert.ToInt32(c);
                                  int NDura =   y + l;
                        // reject a but if a has value 00 it mean that I can't use it right?
                                  string Cost = line.Substring(75, 3);
                                  string Detail = line.Substring(79, 2);
                                  string Network = line.Substring(82, 8);
              

              :) and it work Thx

              G 1 Reply Last reply
              0
              • Y yueru

                Thank you I 've got it even if I've made alot of mistake like u said now is my code

                string Duration = line.Replace("'", "[']");
                string LineOO = line.Replace("#", "[#]");
                string Date = line.Substring(12, 8);
                if (line.Substring(11, 1) == "=")
                {
                string Time = line.Substring(21, 5);
                string Linein = line.Substring(27, 10);
                string Lineout = LineOO.Substring(38, 1);

                                if ((string.Compare(Lineout, "0") > 0 || string.Compare(Lineout, "0") == 0) && (string.Compare(Lineout, "9") < 0 || string.Compare(Lineout, "9") == 0))
                                {
                                   
                                    //int g = line.Length;
                                    //string Number = line.Substring(40, g - 40);
                                    string Number = line.Substring(40, 20);
                                    string Status = line.Substring(61, 3);
                                  string NDuration = Duration.Substring(64, 10);
                                    
                
                                   // string a = Duration.Substring(1, 2);
                                           string b = NDuration.Substring(3, 2);
                                    string c = NDuration.Substring(6, 2);
                                   // int s = Convert.ToInt32(a);
                                    int y = Convert.ToInt32(b);
                                    int l = Convert.ToInt32(c);
                                    int NDura =   y + l;
                          // reject a but if a has value 00 it mean that I can't use it right?
                                    string Cost = line.Substring(75, 3);
                                    string Detail = line.Substring(79, 2);
                                    string Network = line.Substring(82, 8);
                

                :) and it work Thx

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                There is still room for some improvement. ;) Instead of getting a one character string, you can get a character. You can change this:

                if (line.Substring(11, 1) == "=")

                to:

                if (line[11] == '=')

                You can use the >= operator instead of doing both > and == comparisons. You can change this:

                if ((string.Compare(Lineout, "0") > 0 || string.Compare(Lineout, "0") == 0) && (string.Compare(Lineout, "9") < 0 || string.Compare(Lineout, "9") == 0))

                to:

                if (string.Compare(Lineout, "0") >= 0 && string.Compare(Lineout, "9") <= 0)

                However, you can use a char instead of a one character string there too:

                char Lineout = Line00[38];
                if (Lineout >= '0' && Lineout <= '9')

                or simply:

                if (Char.IsDigit(Line00, 38))

                Despite everything, the person most likely to be fooling you next is yourself.

                Y 1 Reply Last reply
                0
                • G Guffa

                  There is still room for some improvement. ;) Instead of getting a one character string, you can get a character. You can change this:

                  if (line.Substring(11, 1) == "=")

                  to:

                  if (line[11] == '=')

                  You can use the >= operator instead of doing both > and == comparisons. You can change this:

                  if ((string.Compare(Lineout, "0") > 0 || string.Compare(Lineout, "0") == 0) && (string.Compare(Lineout, "9") < 0 || string.Compare(Lineout, "9") == 0))

                  to:

                  if (string.Compare(Lineout, "0") >= 0 && string.Compare(Lineout, "9") <= 0)

                  However, you can use a char instead of a one character string there too:

                  char Lineout = Line00[38];
                  if (Lineout >= '0' && Lineout <= '9')

                  or simply:

                  if (Char.IsDigit(Line00, 38))

                  Despite everything, the person most likely to be fooling you next is yourself.

                  Y Offline
                  Y Offline
                  yueru
                  wrote on last edited by
                  #8

                  Wowwww It's so short and simply but I've never knoww. :) thx very much Code is ok but I 've just want to know how to decrease time to do it coz I have 10,100 data and it took a long time.

                  G 1 Reply Last reply
                  0
                  • Y yueru

                    Wowwww It's so short and simply but I've never knoww. :) thx very much Code is ok but I 've just want to know how to decrease time to do it coz I have 10,100 data and it took a long time.

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    I while back I wrote this method that you can use to replace Int32.Parse, and that is about ten times faster. That could help to speed up it a bit.

                    public static int ParseInt32(string text) {
                    long value = 0;
                    long sign = 1;
                    bool first = true;
                    foreach (char c in text) {
                    if (c >= '0' && c <= '9') {
                    value = value * 10 + c - '0';
                    } else if (c == '-' && first) {
                    sign = -1;
                    } else {
                    throw new FormatException();
                    }
                    first = false;
                    }
                    value *= sign;
                    if (value < int.MinValue || value > int.MaxValue) throw new OverflowException();
                    return (int)value;
                    }

                    Despite everything, the person most likely to be fooling you next is yourself.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups