Skip to content

Instantly share code, notes, and snippets.

@haakonn
Created April 10, 2012 18:22
Show Gist options
  • Save haakonn/2353416 to your computer and use it in GitHub Desktop.
Save haakonn/2353416 to your computer and use it in GitHub Desktop.
Utility to concatenate an array of single digits ([1,2,3]) into an int (123)
public class ArrayToIntUtil {
public static int a2i(int... a) {
int result = 0;
int f = (int) Math.pow(10, a.length);
for (int i : a) result += i * (f /= 10);
return result;
}
public static void main(String... args) {
System.out.println(a2i()); // prints 0
System.out.println(a2i(6)); // prints 6
System.out.println(a2i(6, 1, 3, 5, 5, 1)); // prints 613551
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment