YAKOVM
YAKOVM

Reputation: 10153

Convert chars corresponding to digits to uint

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

Upvotes: 1

Views: 2150

Answers (5)

musefan
musefan

Reputation: 48415

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
}

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149020

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);

Upvotes: 0

Stephan Zaria
Stephan Zaria

Reputation: 496

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.

Upvotes: 0

Habib
Habib

Reputation: 223247

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

Upvotes: 2

L.B
L.B

Reputation: 116118

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/

Upvotes: 2

Related Questions