Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Converting from integer value to array not returning the correct, original values

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}.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading