Menu

FIX4NET.Utils.ParseInt() Negative Numbers

jdahl
2009-05-11
2013-04-15
  • jdahl

    jdahl - 2009-05-11

    Not sure if it is intended, but the ParseInt function is only valid for positive integers.

    This is what I mod'd it to:

    public static int ParseInt(string s, int iIndex, int iLen)
    {
        int iStop = iIndex + iLen;
        int iValue = 0;

        if(s[iIndex] == '-')
            iIndex++;

        for (int i = iIndex; i < iStop; i++)
            iValue = (iValue * 10) + (s[i] - 48);

        return s[iIndex] == '-' ? iValue * -1 : iValue;
    }

     
    • jdahl

      jdahl - 2009-05-11

      Sorry, copied wrong code. It should be this:

      public static int ParseInt(string s, int iIndex, int iLen)
              {
                  int iStop = iIndex + iLen;
                  int iValue = 0;
                  int multiplier = 1;
                 
                  if(s[iIndex] == '-')
                  {
                      multiplier = -1;
                      iIndex++;
                  }

                  for (int i = iIndex; i < iStop; i++)
                      iValue = (iValue * 10) + (s[i] - 48);

                  return multiplier < 0 ? iValue * multiplier : iValue;
              }

       

Log in to post a comment.