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.

Discussions

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
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
converting string to decimal digits? - C++ Forum
I'm a beginner C++ student, we just started with arrays and strings. :) Anyhow, the problem I'm having is with a problem I was assigned the other day, involving strings and very long integers (numbers of up to 100 digits). The problem is like this: Problem [Very Long Integer] Definition: A very long integer (VLI) is a nonnegative integer that has no more than 100 decimal ... More on cplusplus.com
🌐 cplusplus.com
String to decimal
How to convert string value 32 into decimal value 32.00 in uipath? More on forum.uipath.com
🌐 forum.uipath.com
0
0
December 29, 2021
🌐
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.
🌐
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..
🌐
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
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'.
🌐
Cplusplus
cplusplus.com › forum › beginner › 85207
converting string to decimal digits? - C++ Forum
If it's longer you could simply truncate the string, but it might be better to issue a warning message and ask the user to try again. d) use a loop to step through the input string one character at a time. Convert the character to the corresponding (integer) decimal digit.
🌐
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?
🌐
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.
🌐
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
🌐
SAP Community
community.sap.com › t5 › application-development-and-automation-discussions › convert-string-to-decimal › td-p › 1059757
Solved: Convert string to decimal - SAP Community
October 20, 2022 - Before moving the string you have to delete the virgola, because the dump occurs: ... NUMBER TYPE P DECIMALS 2. ... REPLACE ',' WITH SPACE INTO NUMBER_C.
🌐
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.