1

I need to travers the string ,which should be the string of digits and make some arithmetic operations on these digits

for (int i = data.Length - 1; i >= 0; --i)
  {
       uint curDigit;
       //Convert  to uint the current rightmost digit, if convert fails return false (the valid data should be numeric)
       try
       {
           curDigit = Convert.ToUInt32(data[i]);
           //arithmetic ops...
       }
       catch
       {
         return false;
       }

I test it with the following input data string. "4000080706200002" For i = 15,corresponding to the rightmost digit 2,I get 50 as an output from

curDigit = Convert.ToUInt32(data[i]);

Can someone please explain me what is wrong?and how to correct the issue

YAKOVM
  • 9,805
  • 31
  • 116
  • 217
  • If you aren't sure if you'll be able to parse the string use `TryParse`, rather than using exceptions for control flow. – Servy Feb 17 '14 at 15:49
  • The `Convert` class is not smart enough to figure out what you want. In fact it is quite useless, there is nothing it does that either simple math or the methods of `System.Byte`, `System.Int32`, etc. don't do better. – Ben Voigt Feb 17 '14 at 15:54

5 Answers5

2

50 is the ascii code for '2'. what you need is '2' - '0' (50-48)

byte[] digits = "4000080706200002".Select(x => (byte)(x - '0')).ToArray();

http://www.asciitable.com/

L.B
  • 114,136
  • 19
  • 178
  • 224
2

What you are getting back is the ascii value of character 2, You can use call ToString on the character item and then call Convert.ToUnit32, Consider the example:

char x = '2';
uint curDigit = Convert.ToUInt32(x.ToString());

this will give you back 2 as curDigit

For your code you can just use:

curDigit = Convert.ToUInt32(data[i].ToString());

Another option is to use char.GetNumericValue like:

uint curDigit = (UInt32) char.GetNumericValue(data[i]);

char.GetNumericValue returns double and you can cast the result back to UInt32

Habib
  • 219,104
  • 29
  • 407
  • 436
0

Your getting the ASCII (or Unicode) values for those characters. The problem is that the code points for the characters '0''9' are not 0 … 9, but 48 … 57. To fix this, you need to adjust by that offset. For example:

curDigit = Convert.ToUInt32(data[i] - '0');

Or

curDigit = Convert.ToUInt32(data[i] - 48);
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
0

The problem is that data[i] returns a char variable, that essentialy is an integer holding the ASCII code of the character. So '2' corresponds to 50. There are 2 things you can do to overcome this behaviour:

  1. Better curDigit = Convert.ToUInt32(data[i] - '0'); //Substract the ASCII representation of '0' from the char
  2. curDigit = Convert.ToUInt32(data.Substring(i,1)); //Use substring to return a string instead of char. Note, that this method is less efficient, as Convert from string essentially splits the string into chars, and substract '0' from each and every one of them.
Stephan Zaria
  • 496
  • 5
  • 12
0

Rather than messing around with ASCII calculations you could use UInt32.TryParse as an alternative solution. However, this method requires a string input not char, so you would have to modify your approach a little:

string input = "4000080706200002";

string[] digits = input.Select(x => x.ToString()).ToArray();

foreach(string digit in digits)
{
    uint curDigit = 0;
    if(UInt32.TryParse(digit, out curDigit))
    {
        //arithmetic ops...
    }
    //else failed to parse
}
musefan
  • 47,875
  • 21
  • 135
  • 185