I have a C# application where I am saving off my array into an integer value, which is working.
here is my array: int[] myArray = { 0, 1, 3, 15 };
However, when I try to convert myArray to see what values it has it returns the following {1, 3, 1, 5} as you can see it doesn’t get the 0, and it separates the 15 into {1, 5}.
Here is my approach:
double[] array = { 0, 1, 3, 15 };
var result = string.Concat(array);
int resultNumber = int.Parse(result); // returns 1315
int[] resultToArray = resultNumber.ToString().Select(o => Convert.ToInt32(o) - 48).ToArray();
What am I missing from my resultToArray?
>Solution :
int[] myArray = { 0, 1, 3, 15 };
string concatenated = string.Join(",", myArray);
Console.WriteLine("Concatenated string: " + concatenated);
int[] resultToArray = concatenated.Split(',').Select(int.Parse).ToArray();
Console.WriteLine("Result array: " + string.Join(", ", resultToArray));
i guess no need to explain much in above code,it’s self explanatory. i just used string.Join(",", myArray) to create a string with each integer separated by a comma and then split it back into an array using Split(‘,’) and parsed each element back to an integer using Select(int.Parse)
this would work fine imo, let me know!