Convert array of integers to single int

Posted on 2006-04-14
Last Modified: 2007-12-19
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?
Question by:chsalvia
    LVL 86

    Accepted Solution

    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;

    LVL 16

    Expert Comment

    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.

    LVL 13

    Assisted Solution

    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.
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now