Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Int List Into Integer

Tags:

c#

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?

like image 958
Kezzer Avatar asked Jun 14 '10 11:06

Kezzer


People also ask

How do I turn a list into an Integer list?

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.


1 Answers

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;
}
like image 158
David M Avatar answered Oct 03 '22 02:10

David M