This is a bit of an odd case I must admit, but I'm trying to find a succinct and elegant way of converting a List<int>
into an actual int
. For example, if my list contained entries 1, 2, and 3, then the final integer would be 123. The only other way I thought of doing it was converting the array to a string and then parsing the string.
Any hints?
To convert a list of strings to a list of integers: Pass the int() class and the list to the map() function. The map() function will pass each item of the list to the int() class. The new list will only contain integer values.
Iterate the list, as if adding a sum, but multiply the running total by 10 at each stage.
int total = 0;
foreach (int entry in list)
{
total = 10 * total + entry;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With