If the string is variable length and null terminated (the usual format for strings in C and Arduino), there's a built-in function to do it. Here's a simple sketch to demonstrate:
Code: Select all
#include <stdlib.h> // for the atol() function
#define numBufSize 11
char numStr[numBufSize] = "3141592654"; // just as an example
void setup()
{
Serial.begin(115200);
// Convert decimal string in numStr to an unsigned integer in num.
// The string must be null-terminated!
unsigned long num;
num = atol(numStr);
Serial.println(num);
}
void loop()
{
}
Code: Select all
#define numBufSize 11
char numStr[numBufSize] = { '0','3','1','4','1','5','9','2','6','5','4' }; // just as an example
void setup()
{
Serial.begin(115200);
// Convert decimal string in numStr to an unsigned integer in num.
// The string is in a fixed size buffer.
// There is no checking for invalid digit characters or number too large.
unsigned long num = 0;
for (int i = 0; i < numBufSize; i++) {
num = num * 10 + (numStr[i] - '0');
}
Serial.println(num);
}
void loop()
{
}