Use the strtoXXX() family of functions. If you need int, long or long long or their unsigned variants:
long l = strtol("1234567", NULL, 10);
long long ll = strtoll("1234567", NULL, 10);
unsigned long l = strtoul("1234567", NULL, 10);
If you need a float, double, or long double use this:
float f = strtof("3.1415927", NULL);
double d = strtod("3.1415927", NULL);
Manuals here and here.
Answer from user529758 on Stack OverflowUse the strtoXXX() family of functions. If you need int, long or long long or their unsigned variants:
long l = strtol("1234567", NULL, 10);
long long ll = strtoll("1234567", NULL, 10);
unsigned long l = strtoul("1234567", NULL, 10);
If you need a float, double, or long double use this:
float f = strtof("3.1415927", NULL);
double d = strtod("3.1415927", NULL);
Manuals here and here.
Usually given a string:
char * myStr= "123";
the way to obtain it's value as an int is:
int value=atoi(myStr);
Some things important to notice:
the following include is necessary:
#include <stdlib.h>
and you must be sure that your string is a null terminated string otherwise atoi will crash you program.
You didn't gave us much information but if you're programming a microcontroller (I suspect that since you told us about a motor) you maybe won't want to use stdlib. In that case you might have use a costum function.
Please take a look at the code bellow:
int stringToInt(char* nrStr){
int nrChars=0;
while(nrStr[nrChars]!='\0'){
nrChars++;
}
int result=0;
int i=0;
while(nrStr[i]!='\0'){//while you dont get to the end of the string
int digit=nrStr[i]-48;//48 is zero ascii code
int exp=nrChars-i-1;
int add=digit*power(10,exp);
result+=add;
i++;
}
return result;
}
int power(int base, int exp){
int ret=1;
int i;
for(i=0;i<exp;i++){
ret*=base;
}
return ret;
}
This does not use any library functions and does the job. I've done it in 3 minutes and it may have some small error, it's not very efficient and does not verify possible errors, but in principle if you pass the strinToint function a well formed integer as a null terminated string it will output the correct value.
If you're using a library that does have some implementation of a power function do use it instead of the one I gave you since it is not efficient at all.
One last note: if you for some reason need to use it in other basis lets say octal basis, you have to chance the line:
int add=digit*power(10,exp);
to:
int add=digit*power(8,exp);
for hexadecimal this will not work, and implementation of such a function will be significantly different.
Videos
Hey everyone, I am currently trying to do some work with Big Numbers in C. Basically, I have an input of string digits in base 10 that is arbitrarily long (longer than long long int), and I am trying to store the data in an array of uint16_t types.
My original plan was to do atoi on individual digits, multiplying it by its location in the string (base 10), and adding it to the 0th value, and then managing overflows from there. So if it was 543, I would put the 3 in the first slot, then add 40 to make it 43 (check for overflow), then add 500 to make it 543 (check for overflow). If there were more numbers, I would keep going, and if there was any instance of overflow I would increment the next significant value by 1, check THAT for overflows, and keep going.
The problem is that when the numbers get really long, I can’t multiply the digit in the string by its place, as that is larger than INT_MAX, or even larger than long long int. How can I find which section of the uint16_t array to put the value to ensure no overflows, and allow me to keep cascading the “carry the 1” idea?
Alternatively, is there a flat out better way to do this? I have seen some stuff online about shortcuts you can take to convert an int to a base 2n, however I don’t think it works if the decimal is stored in string notation.
#include <stdio.h>
#include <stdlib.h>
int i2a(char *s, int n){
div_t qr;
int pos;
if(n == 0) return 0;
qr = div(n, 10);
pos = i2a(s, qr.quot);
s[pos] = qr.rem + '0';
return pos + 1;
}
char* my_itoa(char *output_buff, int num){
char *p = output_buff;
if(num < 0){
*p++ = '-';
num *= -1;
} else if(num == 0)
*p++ = '0';
p[i2a(p, num)]='\0';
return output_buff;
}
int main(void){
int number = -254;
char string[12];
printf("%s", my_itoa(string, number));
return 0;
}
int number = 254;
int back=0,i=0;
char ch='\0';
char string[10];
for(i=0;(number/10)==0;i++){
back=number%10;
ch=(back*pow(10,i)+48);
string[i]=ch;
}
- divide number per 10
- get the rest( with % ) the rest is the digit you want starting from the right
- cast the digit to char +48(ascii 0)
- add to string
Hey everyone, I am currently trying to do some work with Big Numbers in C. Basically, I have an input of string digits in base 10 that is arbitrarily long (longer than long long int), and I am trying to store the data in an array of uint16_t types.
My original plan was to do atoi on individual digits, multiplying it by its location in the string (base 10), and adding it to the 0th value, and then managing overflows from there. So if it was 543, I would put the 3 in the first slot, then add 40 to make it 43 (check for overflow), then add 500 to make it 543 (check for overflow). If there were more numbers, I would keep going, and if there was any instance of overflow I would increment the next significant value by 1, check THAT for overflows, and keep going.
The problem is that when the numbers get really long, I can’t multiply the digit in the string by its place, as that is larger than INT_MAX, or even larger than long long int. How can I find which section of the uint16_t array to put the value to ensure no overflows, and allow me to keep cascading the “carry the 1” idea?
Alternatively, is there a flat out better way to do this? I have seen some stuff online about shortcuts you can take to convert an int to a base 2n, however I don’t think it works if the decimal is stored in string notation.
Lets say i have this string
string s = "33.000";
i need to convert it to decimal with this format
decimal d = 33.000;
I am doing convert.toDecimal but receive decimal formating like this 33.00 so i lost last zero. any idea?