I had to split an int \"123456\" each value of it to an Int[] and i have already a Solution but i dont know is there any better way : My solution was :
publi
private static int[] ConvertIntToArray(int variable)
{
string converter = "" + variable;
int[] convertedArray = new int[converter.Length];
for (int i=0; i < convertedArray.Length;i++) //it can be also converter.Length
{
convertedArray[i] = int.Parse(converter.Substring(i, 1));
}
return convertedArray;
}
We get int via using method. Then, convert it to string
immediately (123456->"123456"). We have a string called converter
and carry to int
value. Our string have a string.Length
, especially same length of int
so, we create an array
called convertedArray
that we have the length, that is converter(string
) length. Then, we get in the loop where we are convert the string to int one by one via using string.Substring(i,1)
, and assign the value convertedArray[i]
. Then, return the convertedArray
.At the main
or any method you can easily call the method.
Integer or Long to Integer Array C# Convert it to char array and subtract 48.
public static int[] IntToArray(int value, int length)
{
char[] charArray = new char[length];
charArray = value.ToString().ToCharArray();
int[] intArray = new int[length];
for (int i = 0; i < intArray.Length; i++)
{
intArray[i] = charArray[i] - 48;
}
return intArray;
}
public static int[] LongToIntArray(long value, int length)
{
char[] charArray = new char[length];
charArray = value.ToString().ToCharArray();
int[] intArray = new int[length];
for (int i = 0; i < intArray.Length; i++)
{
intArray[i] = charArray[i] - 48;
}
return intArray;
}