Converting an int array to an int value in c -
i have integer array
int number[] = {1,2,3,4};
what can int x = 1234? need have c version of it.
x = 1000*number[0] + 100*number[1] + 10*number[2] + number[3];
this how decimal numbers work. more general version (when don't know how long 'number' is) be:
int x = 0; int base = 10; for(int ii = 0; ii < sizeof(number); ii++) x = base*x + number[ii];
note - if base
other 10, above code still work. of course, if printed out x
usual cout<<x
, confusing answer. might serve @ other time. of course want check number[ii]
between 0 , 9, inclusive - that's pretty implied question. still - programming requires checking, checking, , checking. i'm sure can add bit yourself, though.
Comments
Post a Comment