How to Convert Char Array to Int in C++

  1. Use std::strtol Function to Convert Char Array to an Int
  2. Use sscanf() Function to Convert Char Array to an Int

This article will introduce C++ methods to convert a char array to an int type.

Use std::strtol Function to Convert Char Array to an Int

strtol method interprets the first valid characters from char array to an integer type. The function takes the number base for the converted integer as a third parameter, with a value from - {0,2,3,…,36} range. The second parameter is of type char **endptr, which is optional, and if passed stores the address pointed to the character past the last character interpreted.

#include <iostream>
#include <string>

using std::cout; using std::endl;
using std::cin; using std::string;

int main() {
    string str1("23323experimental_string");

    auto str1_n = std::strtol(str1.data(), nullptr, 10);
    printf("%ld", str1_n);

    return EXIT_SUCCESS;
}

Output:

23323

The following example demonstrates the strtol function with the second parameter being non-nullptr. Notice that we are using the printf function only as a type verification tool, and you should use cout in any other cases. It’s also important to implement error handling routines for strtol details of which can be seen here.

#include <iostream>
#include <string>

using std::cout; using std::endl;
using std::cin; using std::string;

int main() {
    string str1("23323experimental_string");
    char *char_part = nullptr;

    auto str1_n = std::strtol(str1.data(), &char_part, 10);
    printf("%ld\n", str1_n);
    printf("%s\n", char_part);;

    return EXIT_SUCCESS;
}

Output:

23323
experimental_string

Use sscanf() Function to Convert Char Array to an Int

sscanf function reads input from the character string buffer and interprets it according to format specifier, which is passed as the second parameter. The numeric value is stored at the pointer to the int variable. Format specifiers are extensively detailed on the sscanf manual page:

#include <iostream>
#include <string>

using std::cout; using std::endl;
using std::cin; using std::string;

int main() {
    string str1("23323experimental_string");

    int str1_n;
    sscanf(str1.data(), "%d", &str1_n);
    printf("%d\n", str1_n);

    return EXIT_SUCCESS;
}

Output:

23323

We can also reimplement the above example to store the non-numeric part of the input string. In this case, we add %s format specifier to the second argument and pass the destination char pointer as the fourth argument. Notice that sscanf is a variadic function as it can take a variable number of arguments from a caller.

#include <iostream>
#include <string>

using std::cout; using std::endl;
using std::cin; using std::string;

int main() {
    string str1("23323experimental_string");

    int str1_n3;
    string str2 {};
    sscanf(str1.data(), "%d%s", &str1_n3, str2.data());
    printf("%d\n", str1_n3);
    printf("%s\n", str2.data());

    return EXIT_SUCCESS;
}

Output:

23323
experimental_string

Related Article - C++ Char

  • How to Convert Int to Char Array in C++
  • How to Convert Char Array to String in C++
  • comments powered by Disqus