That should be fairly easy with the String manipulation routines that accompany VB. There are infact two methods you can follow.
Method 1:[tab][/tab]To start with, you can simple convert the number into a string - and then use the substring function to find out each character. To achieve this we'll have to find the length of the string - which is 5, in your example above - and then iterate through each position of the string, picking up individual characters as we go.
'Say my number is stored in a variable called Num
Dim Num As Integer = 54321
'First I declare two variables - actually one Array to hold each character
'We just assing a random number of elements to the array - having 32
'will help us deal with numbers as long as 32 digits long
Dim Digits (32) As Integer
'And one variable to hold the length of the string
Dim Length As Integer
'I convert it to a String first
Dim NumString As String = CType ( Num, String )
'This NULL-Check is implemented so that if you use an empty string here, the code
'won't crash when it comes to checking for the substring - but instead would
'gracefully skip this part altogether
If NumString <> "" Then
'Now I find the length of the string -
Length = NumString.Length
'Iterate through the string, picking up each character
For Index As Integer = 0 To Length - 1
Digits ( Index ) = CType ( NumString.SubString ( Index, 1 ), Integer )
Next
End If
That's it - the above approach will give you an array called Digits() whose positions indexed by
0 through Length - 1 will contain the individual digits of your number.
[hr=noshade][/hr]
Method 2:[tab][/tab]The second method essentially involves the same principle as above - but computationally is a little faster - as the algorithm is based on numerical computations. A few words on the algorithm first.
[tab][/tab]Take for example, your own number, 54321 - can you think of a concrete method of stripping the number off the first digit "5" - leaving behind only "4321" ??
What would give you 5 as a result, if you divide 54321 by it ? You'd instantly arrive on the figure 10,000.
So: 54321 / 10000 = 5This is all fine - you've got your first digit. But then how do you get the rest of the number together ? Arithmatically, if you divide 54321 by 10000 - we'd have a remainder of 4321 - that's exactly what we need. So how do you find the remainder. Here's where an arithmatic operator called Mod comes into play. It's known as
Mod in terms of Visual Basic - and mostly represented as a
% symbol in languages like C/C++, Java etc.
So: 54321 Mod 10000 = 4321There - we have our first digit extracted, leaving behind the last four. Now if you repeat the above steps, except that you divide and do the Mod with 1,000 instead of 10,000 - you'd yet extract one more digit leaving behind the last 3. Next you repeat the steps again with 100 and then with 10 - and you'd have all your digits separated out.
How do we implement this ? Here's the code:
'Say Num contains the number
Dim Num As Integer = 54321
'Once again - we find the length of the number - which gives us the number of
'digits in it, and then we declare an array based on that number
Dim Length As Integer
If Str ( Num ) <> "" Then
Length = Str ( Num ).Length
End If
'Next we declare two variables and an array
Dim Divisor As Integer = 10 ^ ( Length - 1 )
Dim Index As Integer = 0
Dim Digits ( Length - 1 ) As Integer
'Here we start the Loop
While ( Divisor > 0 )
'Extract the first digit
Digits ( Index ) = Int ( Num / Divisor )
'Extract remainder number - and store it back in Num
Num = Num Mod Divisor
'Decrease Divisor's value by 1/10th units
Divisor /= 10
'Increment Index
Index += 1
End While
Even in this case, your array Digits() will contain the extracted digits starting from position
0 through Length - 1.
[tab][/tab]Try out both and let me know - both should work just fine, giving your the same output. Personally, I find the second method more ELEGANT. The first one is a little blunt - doing things by brute force, although is much easier to understand. However, as I mentioned earlier, the second one computes way faster.
All the best

m^e