Link to home
Start Free TrialLog in
Avatar of directxBOB
directxBOBFlag for Ireland

asked on

Converting a String to Int[]

I have the code below, and I am having some weird behaviour when trying to convert a string to an int array. I know all values in the string are numbers and would just like to convert them into an int array so I can do some LUHN checking.

Some issues

The first digit in convert is 4
when I do:

char test = convert[i];

I get 52'4'

where 52 is the ASCII value for 4.

System.Convert.ToInt32(convert[i])

I get 52 where I am really looking for 4.


If I do:

Any ideas?
public static int[] ConvertString(string convert)
        {
            int[] digits = new int[convert.length];
 
            for (int i = 0; i < convert.Length; i++)
            {
                digits[i] = System.Convert.ToInt32(convert[i]);
            }
 
            return digits;
        }

Open in new window

Avatar of lucky_james
lucky_james
Flag of India image

52'4'....
you need 4...you have got 52.
then you can find for ' and have 4.
you can you IndexOf method of string class, get this 4 in another string and can convert the new formed string to integer.
seems like you want only 4 then you can use :
str.Remove(0, str.IndexOf("'')); // this would remove 52'
str.Substring(0, str.Length -1); // this would remove last '
now str would have 4 only.

hope it helps.
The best way is to use string.Split() method to separate elements, and a List to build the array dynamically:
public static int[] ConvertString(string convert)
{
    string[] elems = convert.Split('\'');
    List<int> list = new List<int>;
 
    foreach (string s in elems)
    {
         if (s.Length > 0)
              list.Add(Convert.ToInt32(s));
    }
    return list.ToArray();
}

Open in new window

I believe that this function will be better for you I have just made.  This will only add digits to an int array and not fallover if your string to convert is l9ike 123456H

Andrew
        static int[] ConvertString(string convert)
        {
            List<int> intList = new List<int>();
            foreach (char c in convert)
            {
                if (char.IsDigit(c))
                    intList.Add((Int32)Char.GetNumericValue(c));
            }
            return intList.ToArray();
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of directxBOB

ASKER

char x = 9;    //Character '9' = ASCII 57
int b;

b = x - '0';   //That is '9' - '0' = 57 - 48 = 9
Bit of useless info but comparing both my function and that of evilrix , which both work I might point out, BUT

I am surprised to see that evilrix's function is on average 3.9 milliseconds faster than mine.  

foreach loops just have performance hits everywhere lol
I suspect the difference is the overhead in calling Char.GetNumericValue and Char.IsDigit
true :-)