C# Converting integer into array of single digits in C#?

  • Thread starter Thread starter rollcast
  • Start date Start date
  • Tags Tags
    Array Integer
Click For Summary
To convert an integer in C# into an array of its digits, the most straightforward method involves converting the integer to a string, then to a character array, and finally parsing each character back into an integer. The recommended code for this process is: int[] digits = 12345.ToString().ToCharArray().Select(x => (int)Char.GetNumericValue(x)).ToArray();This approach ensures that each character is correctly converted to its corresponding numeric value. For cases where the input may include non-numeric characters, a regex can be employed to filter out these characters before conversion. The regex method is:int[] digits = Regex.Replace(numberToParse, "[^0-9]", "").Select(x => (int)Char.GetNumericValue(x)).ToArray();This allows for robust handling of various input formats while achieving the desired output of an integer array representing the digits of the original number.
rollcast
Messages
403
Reaction score
0
Whats the easiest way to take an integer in C# and convert it into an array of length equal to the number of digits and each element is a digit from the integer?

EG. If I had the integer 12345 I want to convert it to an array like so {1, 2, 3, 4, 5}

Thanks
AL
 
Technology news on Phys.org
rollcast said:
Whats the easiest way to take an integer in C# and convert it into an array of length equal to the number of digits and each element is a digit from the integer?

EG. If I had the integer 12345 I want to convert it to an array like so {1, 2, 3, 4, 5}

Thanks
AL

I don't know if it's easiest, but in C I would use the modulus operator %. Can you see how you would use it to do this task?
 
berkeman said:
I don't know if it's easiest, but in C I would use the modulus operator %. Can you see how you would use it to do this task?

Nope, :confused:
 
Converting 12345 to an integer array:

Code:
int[] digits = 12345.ToString().ToCharArray().Select(Convert.ToInt32).ToArray();

If you only need a character array you can obviously stop after the ToCharArray().
 
Filip Larsen said:
Converting 12345 to an integer array:

Code:
int[] digits = 12345.ToString().ToCharArray().Select(Convert.ToInt32).ToArray();

If you only need a character array you can obviously stop after the ToCharArray().

The conversion to an int array is not quite right. The Convert.ToInt32 will convert the char to its equivalent decimal value which is not the same as parsing it. e.g. the character '1' will be converted to 49. Instead you will have to use the int.Parse which will require casting the char to a string first but the better approach is to use the Char.GetNumericValue and cast to int16, int32 as preferred because the method returns a double.

The code should look like:


Code:
int[] digits = 12345.ToString().ToCharArray().Select(x => (int)Char.GetNumericValue(x)).ToArray();

To take it to the next level and you want to convert an unknown string you can use regex to strip out non numeric characters.

Code:
int[] digits = Regex.Replace(numberToParse, "[^0-9]", "").Select(x => (int)Char.GetNumericValue(x)).ToArray();
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K