Converting integer into array of single digits in C#?

  • Context: C# 
  • Thread starter Thread starter rollcast
  • Start date Start date
  • Tags Tags
    Array Integer
Click For Summary

Discussion Overview

The discussion focuses on methods to convert an integer into an array of its individual digits in C#. Participants explore various approaches, including string manipulation and mathematical operations.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant asks for the easiest way to convert an integer into an array of its digits, providing an example with the integer 12345.
  • Another participant suggests using the modulus operator (%) as a potential method, although they do not provide a complete solution.
  • A different participant provides a code snippet using string conversion and LINQ to create an integer array from the digits of the number.
  • Another participant critiques the initial approach, noting that using Convert.ToInt32 on characters results in incorrect values and suggests using int.Parse or Char.GetNumericValue instead, providing an alternative code snippet that includes regex for stripping non-numeric characters.

Areas of Agreement / Disagreement

Participants express differing views on the best method to achieve the conversion, with no consensus on a single approach being established.

Contextual Notes

There are unresolved issues regarding the efficiency and correctness of the proposed methods, particularly concerning the conversion of characters to integers and the handling of non-numeric characters.

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

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 25 ·
Replies
25
Views
3K
  • · 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