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
🌐
Online String Tools
onlinestringtools.com › convert-string-to-decimal
Convert a String to Decimal – Online String Tools
This tool converts a ASCII, Unicode or UTF8 string to its decimal representation.
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.

Discussions

c# - Convert string to decimal, keeping fractions - Stack Overflow
I am trying to convert 1200.00 to decimal, but Decimal.Parse() removes .00. I've tried some different methods, but it always removes .00, except when I supply a fraction different than 0. string v... More on stackoverflow.com
🌐 stackoverflow.com
convert string to decimal - C++ Forum
Hi everyone , i have a text file with strings inside it. now i need to read that file and convert the strings into a decimal value. like for example i have the string CSC now i should have the integer values for C S and C . i tried to use strtol and strtoul but it gives me error. More on cplusplus.com
🌐 cplusplus.com
May 11, 2018
Convert String to decimal
Is it possible to Convert String to decimal ASCII ? Note:I am not talking about char to decimal ascii More on forum.arduino.cc
🌐 forum.arduino.cc
1
0
July 3, 2021
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 More on ccsinfo.com
🌐 ccsinfo.com
🌐
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.
🌐
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))...
I heard atoi() converts a string, or character into an integer, but whatever character, or string i give it, it keeps returning 0, not the decimal equivalent. Any help would be much appreciated. Thanks. Blueprint. Last edited by Blueprint; 01-23-2005 at 10:05 PM. ... char somechar = 'a'; int charval = somechar; printf("%c = %d\n",somechar,charval); To do that for an entire string, just loop through the above code for each char in the string.
🌐
Cplusplus
cplusplus.com › forum › beginner › 236749
convert string to decimal - C++ Forum
May 11, 2018 - 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..
Find elsewhere
🌐
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.
🌐
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
🌐
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 =...
🌐
GitHub
github.com › twitter-forks › mysql › blob › master › strings › decimal.c
mysql/strings/decimal.c at master · twitter-forks/mysql
Convert string to decimal · · SYNOPSIS · internal_str2decl() from - value to convert. Doesn't have to be \0 terminated! to - decimal where where the result will be stored · to->buf and to->len must ...
Author   twitter-forks
🌐
C For Dummies
c-for-dummies.com › blog
From Decimal Value to a String | C For Dummies Blog
May 14, 2022 - 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.
🌐
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.
🌐
UiPath Community
forum.uipath.com › help › activities
String to decimal - Activities - UiPath Community Forum
December 29, 2021 - How to convert string value 32 into decimal value 32.00 in uipath?
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.convert.todecimal
Convert.ToDecimal Method (System) | Microsoft Learn
Dim numbers() As UShort = { UInt16.MinValue, 121, 12345, UInt16.MaxValue } Dim result As Decimal For Each number As UShort In numbers result = Convert.ToDecimal(number) Console.WriteLine("Converted the UInt16 value {0} to {1}.", _ number, result) Next ' The example displays the following output: ' Converted the UInt16 value 0 to 0. ' Converted the UInt16 value 121 to 121. ' Converted the UInt16 value 12345 to 12345. ' Converted the UInt16 value 65535 to 65535. ... Converts the specified string representation of a number to an equivalent decimal number, using the specified culture-specific formatting information.
🌐
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?
August 25, 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.
🌐
PhraseFix
phrasefix.com › tools › string-to-decimal
String to Decimal Converter - PhraseFix
Free online tool that converts strings to decimal numbers. Simply enter the text and the online tool will convert it into decimal numbers.