• C

Convert array of integers to single int

Suppose I have an array of single-digit, base 10 integers, like:

int intarray = { 2 , 5, 7 };

Is there a way I can convert this into a single int, with a value of 257?
chsalviaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You mean like

int a2int(int* arr, size_t sz) {

int ret = 0;

 for (int i = 0; i < sz; ++i) {

  ret *= 10;

  ret += arr[i];
 }

return ret;
}

?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulCaswellCommented:
Hi chsalvia,

Could you please post what you have tried and we can help you understand what you are doing wrong or help you out with the parts you find difficult.

Paul
Kelvin_KingCommented:
You could do it.

Firstly, each array represents a power of ten.

So lets say you have an int array [1 2 3 4 5]

this means, your integer will be ( working backwards ):
(5 * 10 ^ 0) + (4 * 10 ^ 1) + (3 * 10 ^ 2) + (2 * 10 ^ 3) + (1 * 10 ^ 4)

so if you see the pattern you could write a simple for loop which iterates through the int array, multiply it by it's 10's coefficient, and add it to the total sum.

Good luck with it.
jkrCommented:
;o)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.