[Solved] Four 1byte char to One 4byte int? [closed]


Try following code:

#include<iostream>
#include<cstring>

using namespace std;

#define CHAR_ARRAY_SIZE 8

int main()
{
    char charArray[CHAR_ARRAY_SIZE] = {'1', '2', '3', '4', '5', '6', '7', '8'};

    int intArray[2];
    for(int i = 0; i < CHAR_ARRAY_SIZE/4; i++) {
        char ch1 = (charArray[4*i+0] - '0');
        char ch2 = (charArray[4*i+1] - '0');
        char ch3 = (charArray[4*i+2] - '0');
        char ch4 = (charArray[4*i+3] - '0');

        intArray[i] = ch1*1000 + ch2*100 + ch3*10 + ch4;
    }
    cout << "intArray[0] : " << intArray[0] << endl;
    cout << "intArray[1] : " << intArray[1] << endl;

    return 0;
}

solved Four 1byte char to One 4byte int? [closed]