I personally don't like atoi function. I would suggest sscanf:
char myarray[5] = {'-', '1', '2', '3', '\0'};
int i;
sscanf(myarray, "%d", &i);
It's very standard, it's in the stdio.h library :)
And in my opinion, it allows you much more freedom than atoi, arbitrary formatting of your number-string, and probably also allows for non-number characters at the end.
EDIT
I just found this wonderful question here on the site that explains and compares 3 different ways to do it - atoi, sscanf and strtol. Also, there is a nice more-detailed insight into sscanf (actually, the whole family of *scanf functions).
EDIT2
Looks like it's not just me personally disliking the atoi function. Here's a link to an answer explaining that the atoi function is deprecated and should not be used in newer code.
I personally don't like atoi function. I would suggest sscanf:
char myarray[5] = {'-', '1', '2', '3', '\0'};
int i;
sscanf(myarray, "%d", &i);
It's very standard, it's in the stdio.h library :)
And in my opinion, it allows you much more freedom than atoi, arbitrary formatting of your number-string, and probably also allows for non-number characters at the end.
EDIT
I just found this wonderful question here on the site that explains and compares 3 different ways to do it - atoi, sscanf and strtol. Also, there is a nice more-detailed insight into sscanf (actually, the whole family of *scanf functions).
EDIT2
Looks like it's not just me personally disliking the atoi function. Here's a link to an answer explaining that the atoi function is deprecated and should not be used in newer code.
Why not just use atoi? For example:
char myarray[4] = {'-','1','2','3'};
int i = atoi(myarray);
printf("%d\n", i);
Gives me, as expected:
-123
Update: why not - the character array is not null terminated. Doh!
C Converting Char array (strings) into int array - Stack Overflow
char - convert array of characters to array of integers in C - Stack Overflow
c - Convert char array to int array? - Stack Overflow
c++ - How to convert array of chars to array of ints? - Arduino Stack Exchange
You might be overthinking things. Here's a simply way to input numbers from a user that doesn't involve formatting or parsing strings.
int numArray[31];
int i = 0;
int num = 0;
while( scanf("%d", &num) > 0 && i < 31 && num >0) {
numArray[i] = num;
i++;
}
The loop will stop when the user enters a negative number, or has entered all 31 possible numbers.
You still have to make sure the user enters a number between 0 and 31 though!
You can use a library or you can write a helper function. The helper function would look something like this (pseudocode):
int output = 0;
for (int i = 0; i < string.length; i++) {
output = output * 10;
output = output + parse(string[i]) //parse first character into a single digit
}
For example, if you have 253, it will read 2. Then it will multiply it by 10 (20) and add 5 (25). Then it will multiply that by 10 (250) and add 3, which will be the expected output.
Note that you need to find a way to parse a single character of your string into a single digit integer - that should be very easy using something like a switch() statement.
EDIT: If you are looking for a library function, check out this question:
What is the difference between sscanf or atoi to convert a string to an integer?
Use sscanf, or strtol in a loop.
The traditional but deprecated way to do this would be to use strtok(). The modern replacement is strsep(). Here's an example straight off the man page for strsep():
char **ap, *argv[10], *inputstring;
for (ap = argv; (*ap = strsep(&inputstring, " \t")) != NULL;)
if (**ap != '\0')
if (++ap >= &argv[10])
break;
That breaks inputstring up into pieces using the provided delimiters (space, tab) and iterates over the pieces. You should be able to modify the above to convert each piece into an int using atoi(). The main problem with strsep() is that it modifies the input string and is therefore not thread safe.
If you know that the input string will always contain the same number of ints, another approach would be to use sscanf() to read all the ints in one go:
char *input = "12 23 45 9";
int output[5];
sscanf(inputstring, "%d %d %d %d %d", &output[0], &output[1], &output[2], &output[3], &output[4]);
Despite using some C++ features, most of your code looks more like C. Might I recommend something more like:
struct to_int {
int operator()(char c) { return c - '0'; }
};
const int num = 67;
std::vector<char> patrons(num);
std::vector<int> patron_values(num);
std::ifstream archivo("1_0.txt");
archivo.read(&patrons[0], num);
std::cout.write(&patrons[0], num);
std::transform(patrons.begin(), patrons.end(), patron_values.begin(), to_int());
std::copy(patron_values.begin(), patron_values.end(),
std::ostream_iterator<int>(std::cout, "\n"));
That's because atoi receives a null-delimited string, while you are passing it a pointer to a single char (both are essentially char *, but the intent and use are different).
Replace the call to atoi with iPatrons[i] = vectorPatron[i] - '0';
Also, you can remove the vectorPatrons array, simply read into a single char in the first loop and then assign to the appropriate place in the iPatrons array.
scanf("%s", &string[0]);
read into input character array. you are reading into some other variable.
scanf("%s",input);
or even best
fgets(input,sizeof input, stdin );
digit[x++] = input[y++]-'0';
// substract `'0'` from your character and assign to digit array.
For example if input[i]=='3' ==> '3'-'0' will result in integer digit 3
I would suggest you to use strtol or sscanf in a loop. That would make things easier for you.
Something like this:
char *c = "12 23 45 9";
int i[5];
sscanf(inputstring, "%d %d %d %d %d", &i[0], &i[1], &i[2], &i[3], &i[4]);
Sample example using atoi() which another option:
int main(int argc,char *argv[]){
char y[10] = "0123456789";
char x[3];
int i;
x[0] = y[8];
x[1] = y[9];
x[2] = '\0';
i=atoi(x);
}
I'm trying to store a hexadecimal number into a malloc'd int variable digit by digit, but I'm confused. In hex numbers your digits go from 0 to F, but how can one store chars as ints?
Even if you could store a char as an int, A-F all represent two-digit values (10-15), so it doesn't seem possible that one can store a hex number as an int, digit by digit.
Can anyone offer hints? I'm really stuck here.