Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int array to int number in Java

Tags:

java

arrays

int

I'm new on this site and, if I'm here it's because I haven't found the answer anywhere on the web and believe me: I've been googling for quite a time but all I could find was how to convert a number to an array not the other way arround. I'm looking for a simple way or function to convert an int array to an int number. Let me explain for example I have this :

int[] ar = {1, 2, 3};

And I want to have this:

int nbr = 123;

In my head it would look like this (even if I know it's not the right way):

int nbr = ar.toInt(); // I know it's funny

If you have any idea of how I could do that, that'd be awesome.

like image 779
gotgot1995 Avatar asked Jan 28 '14 20:01

gotgot1995


3 Answers

Start with a result of 0. Loop through all elements of your int array. Multiply the result by 10, then add in the current number from the array. At the end of the loop, you have your result.

  • Result: 0
  • Loop 1: Result * 10 => 0, Result + 1 => 1
  • Loop 2: Result * 10 => 10, Result + 2 => 12
  • Loop 3: Result * 10 >= 120, Result + 3 => 123

This can be generalized for any base by changing the base from 10 (here) to something else, such as 16 for hexadecimal.

like image 115
rgettman Avatar answered Sep 22 '22 12:09

rgettman


You have to cycle in the array and add the right value. The right value is the current element in the array multiplied by 10^position.

So: ar[0]*1 + ar[1]*10 + ar[2] *100 + .....

    int res=0;
    for(int i=0;i<ar.length;i++) {
         res=res*10+ar[i];
    }

Or

   for(int i=0,exp=ar.length-1;i<ar.length;i++,exp--)
        res+=ar[i]*Math.pow(10, exp);
like image 34
Alessio Avatar answered Sep 24 '22 12:09

Alessio


First you'll have to convert every number to a string, then concatenate the strings and parse it back into an integer. Here's one implementation:

int arrayToInt(int[] arr)
{
    //using a Stringbuilder is much more efficient than just using += on a String.
    //if this confuses you, just use a String and write += instead of append.
    StringBuilder s = new StringBuilder(); 

    for (int i : arr)
    {
         s.append(i); //add all the ints to a string
    }

    return Integer.parseInt(s.toString()); //parse integer out of the string
}

Note that this produce an error if any of the values past the first one in your array as negative, as the minus signs will interfere with the parsing.

This method should work for all positive integers, but if you know that all of the values in the array will only be one digit long (as they are in your example), you can avoid string operations altogether and just use basic math:

int arrayToInt(int[] arr)
{
    int result = 0;

    //iterate backwards through the array so we start with least significant digits
    for (int n = arr.length - 1, i = 1; n >= 0; n --, i *= 10) 
    {
         result += Math.abs(arr[n]) * i;
    }

    if (arr[0] < 0) //if there's a negative sign in the beginning, flip the sign
    {
        result = - result;
    }

    return result;
}

This version won't produce an error if any of the values past the first are negative, but it will produce strange results.

There is no builtin function to do this because the values of an array typically represent distinct numbers, rather than digits in a number.

EDIT:

In response to your comments, try this version to deal with longs:

long arrayToLong(int[] arr)
{
    StringBuilder s = new StringBuilder(); 

    for (int i : arr)
    {
         s.append(i);
    }

    return Long.parseLong(s.toString());
}

Edit 2:

In response to your second comment:

int[] longToIntArray(long l)
{
    String s = String.valueOf(l);   //expand number into a string

    String token;

    int[] result = new int[s.length() / 2]; 

    for (int n = 0; n < s.length()/2; n ++) //loop through and split the string
    {
        token = s.substring(n*2, (n+2)*2);
        result[n] = Integer.parseInt(token); //fill the array with the numbers we parse from the sections
    }

    return result;
}
like image 21
ApproachingDarknessFish Avatar answered Sep 21 '22 12:09

ApproachingDarknessFish