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 Overflow
Top answer
1 of 2
4

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.

2 of 2
1

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.

🌐
Cprogramming
cboard.cprogramming.com › c-programming › 60937-convert-char-string-decimal-equivalent-eg-=-chr-97-a.html
Convert char string to decimal equivalent (eg: a = chr(97))...
char arbit[40] = "dir"; //check we have 4 arguments in total. if (argc != 4) { printf("Incorrect usage!\n"); return 0; } //add argv[1] to the end of the ichar string. strcat (ichar, argv[1]); //run the command. system(ichar); //convert string to decimal. /*THE FOLLOWING DOESNT WORK char my_string[] = "a"; printf("%s = %d\n", my_string); printf ("%s\n", ichar); SO IT'S COMMENTED OUT*/ return 0; } Any ideas?
🌐
Cplusplus
cplusplus.com › forum › beginner › 236749
convert string to decimal - C++ Forum
in c++ characters are integers, 1 byte in length. there is no 'conversion' apart from forcing it to print the integer instead of the character value, which you can do with (int) variable cast in the print: cout << (int)var << endl; or for a string..
🌐
Udemy
blog.udemy.com › home › c string to int: simple ways to convert to numeric values
C String to Int: Simple Ways to Convert to Numeric Values - Udemy Blog
October 25, 2025 - In this case, the string is an array of characters pointed to by num. Then, we calculate the length of the string using the strlen() function. Next, we loop through the string and convert the string into decimal values.
🌐
Online String Tools
onlinestringtools.com › convert-string-to-decimal
Convert a String to Decimal – Online String Tools
Free online string to decimal converter. Just load your string and it will automatically get converted to a decimal string. There are no intrusive ads, popups or nonsense, just a string to decimal converter. Load a string, get its decimal representation.
🌐
CCS, Inc.
ccsinfo.com › forum › viewtopic.php
CCS :: View topic - string to decimal conversion
Profile Log in to check your private messages Log in · CCS does not monitor this forum on a regular basis
Find elsewhere
🌐
Reddit
reddit.com › r/learnprogramming › how to convert a large decimal number in string form to base 2^16 in c?
r/learnprogramming on Reddit: How to convert a large decimal number in string form to base 2^16 in C?
May 6, 2022 -

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.

🌐
YouTube
youtube.com › codevault
How to convert strings to numbers in C - YouTube
Converting strings to numbers in C is a task we might meet frequently, in this video we'll show you the most accepted solution throughout the community.Check...
Published   February 2, 2020
Views   69K
🌐
Reddit
reddit.com › r/c_programming › how to convert a large decimal number in string form to base 2^16 in c?
r/C_Programming on Reddit: How to convert a large decimal number in string form to base 2^16 in C?
May 5, 2022 -

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.

Top answer
1 of 2
7
Option 1: use a "big math" library. Option 2: roll your own basic addition routines, and build from there. Your big numbers will represented by a uint16_t[]. You add two base-N numbers by: adding the 1's column (potentially producing an overflow if the result exceeds N), then adding the N's column (potentially with the overflow from above, and potentially producing another overflow if the result exceeds N2) then adding the N2 's column, etc. etc. In your format, your 1's column is stored in array index [0], your N's columns is stored in [1], your N2 's column is stored in [2], etc. Adding two numbers So to add two uint16_t[] numbers, all you need to do is: add the two [0] values together, and if this exceeds your base (216) then you set an overflow flag, otherwise clear the overflow flag. add the two [1] values and the previous overflow together, and if this exceeds your base (216) then you set an overflow flag, otherwise clear the overflow flag. add the two [2] values and the previous overflow together, and if this exceeds your base (216)... ...etc. etc... eventually you run hit the end of the input arrays, and if the overflow is set then you need to extend the output array length by 1, and put the overflow there. Multiplying numbers Construct a lookup table, up to the maximum number of decimal digits you expect to handle. You can readily extend this to "arbitrary number of digits" but for now maybe cap it at 20 digits or so to prove the process. Construct a uint16_t[] number for "0" (your array is all zeros) Construct a uint16_t[] number for "1" (your array is all zeros, except for index [0] = 1) Save this in a lookup table at position [0] Construct a uint16_t[] number for "10" by adding your "1" to your "0" ten times. Save this in a lookup table at position [1] Construct a uint16_t[] number for "100" by adding your "10" to your "0" ten times. Save this in a lookup table at position [2] Construct a uint16_t[] number for "1000" by adding your "100" to your "0" ten times. Save this in a lookup table at position [4] Construct a uint16_t[] number for "10000" by adding your "1000" to your "0" ten times. Save this in a lookup table at position [4] ...etc. (Here you're obviously using your "add" routine that you wrote earlier! So now you have a lookup table of uint16_t[], where each subsequent index representing the next power of ten. Parse your base-10 text input Read your text input per-digit, 1's digit first, moving up through all the digits. Construct a uint16_t[] number for "0" (your array is all zeros). This will be your running sum that will eventually be your final output. read the 1's digit, and whatever its value is how many times you add to your running sum from position [0] of your lookup created in the "multiply" section above read the 10's digit, and whatever its value is how many times you add to your running sum from position [1] of your lookup created in the "multiply" section above read the 100's digit, and whatever its value is how many times you add to your running sum from position [2] of your lookup created in the "multiply" section above ... etc. (Again you're obviously using your "add" routine that you wrote earlier) Below 232, your uint16_t[] (if stacked end-on-end) should be bitwise identical to a uint32_t Below 264, your uint16_t[] (if stacked end-on-end) should be bitwise identical to a uint64_t
2 of 2
3
b * 10 equals (b * 2 * 2 + b) * 2. Maybe you can do with just longhand addition and a routine to shift left 1 bit.
🌐
TechOnTheNet
techonthenet.com › c_language › standard_library_functions › stdlib_h › strtod.php
C Language: strtod function (Convert String to Double)
/* Example using strtod by TechOnTheNet.com */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> int main(int argc, const char * argv[]) { /* Define temporary variables */ char value[10]; char *eptr; double result; /* Copy a value into the variable */ /* It's okay to have whitespace before the number */ strcpy(value, " 958"); /* Convert the provided value to a double */ result = strtod(value, &eptr); /* If the result is 0, test for an error */ if (result == 0) { /* If the value provided was out of range, display a warning message */ if (errno == ERANGE) printf("The
🌐
C For Dummies
c-for-dummies.com › blog
From Decimal Value to a String | C For Dummies Blog
Variable buffer is declared as static at Line 6 to ensure that its contents aren’t discarded when the function returns. The snprintf() function at Line 9 converts float value a into a string in buffer. The while loop at Line 12 locates the decimal within the string, the dividing character between the value’s integer and fractional part.
🌐
GitHub
github.com › twitter-forks › mysql › blob › master › strings › decimal.c
mysql/strings/decimal.c at master · twitter-forks/mysql
dec->intg= digits_int; dec->frac= digits_frac; return err; } · · /* Convert string to decimal · · SYNOPSIS · internal_str2decl() from - value to convert.
Author   twitter-forks
🌐
Reddit
reddit.com › r/csharp › convert string to decimal keeping fractions.
r/csharp on Reddit: Convert string to decimal keeping fractions.
November 11, 2020 -

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?

Top answer
1 of 5
7
Display format is only relevant when you display it. To convert a string to a decimal, you'd just do: var dec = Convert.ToDecimal("33.000"); When you display it, you could do: Console.WriteLine(dec.ToString("###,###.000"));
2 of 5
3
33.000 and 33.0 are, depending on how a value is stored, the same or not the same. With strings, "33.000" and "33.0" are obviously not the same, while 33.000 and 33.0 are absolutely equal if we're talking about floats. But how do you think decimals are stored? The answer you can find in this stackoverflow question. Basically it's stored as 1 sign bit + a 96 bit integer + 31 bit to say how much it is shifted to the right. That means, it's possible to store 33.000 as (+, 33000, 0) or (+ 330000, 1) etc. To see the binary representation of a decimal, you can use the method GetBits(), you'll get an array of four ints. Armed with this, you can check if the amount of digits you see printed are event dependent on the binary representation. And the answer is: yes it is. If not, you need to format the decimal manually, when you print it. You can create your own format extension method for decimals to not have code duplication. If the amount of digits is dependent on the binary representation, then you either need to write your own parsing method, that handles parsing how you want it to, or you need to check if convert.ToDecimal can be configured somehow, maybe via additional parameters. I just checked and I got the amount of digits from the string correctly Console.WriteLine(Convert.ToDecimal("33,000")); Console.WriteLine(Convert.ToDecimal("33,0")); Console.WriteLine(decimal.Parse("33,000")); Console.WriteLine(decimal.Parse("33,0")); Can you check that?¨ You might have to replace the commas for dots.
🌐
Quora
quora.com › Can-we-directly-convert-a-binary-string-into-a-decimal-in-C++
Can we directly convert a binary string into a decimal in C++? - Quora
Answer (1 of 4): Actually you can do this very easily . [code]#include #include // For pow() using namespace std; //Binary conversion of 70 int main() { // your code goes here string binaryString = "1000110"; //70 in Binary int value = 0; int indexCounter = 0; for(int i =...
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › string-to-numeric-value-functions
String to numeric value functions | Microsoft Learn
If the first character is '1' through '9', the string is interpreted as a decimal integer. The letters 'a' through 'z' (or 'A' through 'Z') are assigned the values 10 through 35; only letters whose assigned values are less than base are permitted.
🌐
Educative
educative.io › answers › how-to-convert-a-string-to-a-decimal-in-c-sharp
How to convert a string to a decimal in C#
Converting a string to a decimal value or decimal equivalent can be done using the Decimal.TryParse() method.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › convert-string-to-int-in-c
Convert String to int in C - GeeksforGeeks
July 23, 2025 - Start traversing the string str from the front. For each character ch, check if they are numeric or non-numeric ASCII value using the range of numbers i.e. [48, 57]. ... Convert it to corresponding integer value using offset (-48) or simply '0'.