🌐
Delft Stack
delftstack.com › home › howto › c string to int
So konvertieren Sie einen String in eine Ganzzahl in C
February 2, 2024 - The atoi() function neglects all white spaces at the beginning of the string, converts the characters after the white spaces, and then stops when it reaches the first non-number character. The atoi() function returns the integer representation of the string.
🌐
GitHub
gist.github.com › shamiul94 › 02d4957f410b138ef8aca6b7602d12b6
String to Integer Conversion.c · GitHub
Save shamiul94/02d4957f410b138ef8aca6b7602d12b6 to your computer and use it in GitHub Desktop. ... This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Discussions

String zu int umwandeln in C
Hallo, in dem folgenden Programm muss ich String zu int umwandeln. Also ich soll eine Funktion aufrufen, aber keine atoi. welche Funktion soll ich also aufrufen, um die Datentypen umzuwandeln?? #include #include int main (){ int i; char buffer [256]; printf ("Geben Sie eine Zahl... More on tutorials.de
🌐 tutorials.de
8
0
August 6, 2020
Help converting string elements to int in C++
First of all, using .open and .close is unnecessary, we have constructors and destructors automatically doing that for you, this isn't C. std::vector> Vectors of vectors are basically never a good idea. You also don't need a 2D structure in this case. You can simply read in a line and then use stoi to do the conversion. std::vector numbers; std::string line; while (std::getline(read, line)) { numbers.push_back(std::stoi(line, nullptr, 2)); } The only thing you need to do with strings for day 3 is parsing them as a binary number and stoi does that for you. After that all you care about are the numbers, you should read about bitwise operations . More on reddit.com
🌐 r/cpp_questions
4
4
December 11, 2021
How can I convert a string to integer in C? - Stack Overflow
I am trying to find out if there is an alternative way of converting a string to an integer in C. I regularly pattern the following in my code. char s[] = "45"; int num = atoi(s); Is th... More on stackoverflow.com
🌐 stackoverflow.com
String zu Int
Hallo zusammen, Ich versuche krampfhaft einen String in eine Int Variable zu verschieben. Komme dabei allerdings nicht weiter. Kann mir jemand einen Denkanstoß / Befehl geben? Möglicher Code String Zahl="101"; int N… More on forum.arduino.cc
🌐 forum.arduino.cc
10
0
March 5, 2012
🌐
C-HowTo
c-howto.de › home › tutorial start › strings (zeichenketten) › typumwandlung
Typumwandlung | C-HowTo
May 7, 2019 - String zu int: atoi · String zu long int: atol · String zu double: atof · Die Funktions-Prototypen dazu: int atoi(const char *string); double atof(const char *string); long atol(const char *string); Hier ein Beispiel für die Konvertierung von ganzen Zahlen mit atoi.
🌐
Tutorials.de
tutorials.de › startseite › foren › programming › c/c++
String zu int umwandeln in C | tutorials.de
August 6, 2020 - Hallo, in dem folgenden Programm muss ich String zu int umwandeln. Also ich soll eine Funktion aufrufen, aber keine atoi. welche Funktion soll ich also aufrufen, um die Datentypen umzuwandeln?? #include <stdio.h> #include <stdlib.h> int main (){ int i; char buffer [256]; printf ("Geben Sie eine Zahl ein"); fgets (buffer, 256, stdin); i = ______; // Funktionsaufruf (mit Parameter[n]).
🌐
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'.
🌐
Stacklounge
stacklounge.de › 4840 › programmieren-in-c-string-in-int-umwandeln
Programmieren in C: String in int umwandeln | Stacklounge
August 24, 2019 - Hallo zusammen! Wir sollen die Lücke füllen. In Zeile 9 soll ein String in einen int umgewandelt werden. Geben Sie ... ist: %d.",i); return 0; }
🌐
Microsoft Learn
learn.microsoft.com › de-de › dotnet › csharp › programming-guide › types › how-to-convert-a-string-to-a-number
Konvertieren einer Zeichenfolge in eine Zahl - C# | Microsoft Learn
string wird in eine Zahl umgewandelt, indem Sie die Methode Parse oder TryParse für numerische Typen (z. B. int, long und double) oder Methoden in der System.Convert-Klasse aufrufen.
🌐
Reddit
reddit.com › r/cpp_questions › help converting string elements to int in c++
r/cpp_questions on Reddit: Help converting string elements to int in C++
December 11, 2021 -

I was attempting to read in binary numbers from a text file to manipulate in my code. I decided to take the approach to read them in as a string using the `std::getline()` function. I then stored the numbers into a vector of strings. Then I wanted to convert them into a vector of a vector of integers, so it would be easier (in my head) to sort through.

My issue is that I want to convert the string elements to integers> I tried using std::stoi(), but I continue getting the following error :no matching function for call to 'stoi(__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&, int, int)'

num = std::stoi(bits[j].at(i));

I'm not sure what the issue is. Am I using `std::stoi` wrong? is my understanding of how to manipulate the elements in the string incorrect? Any help that you can provide would be appreciated. See below for a snippet of the code that I am trying to work on.

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>

int main(){
    std::ifstream read;
    std::string line;
    std::vector<std::string> bits;
    std::vector<std::vector<int>> vint[5];
    std::vector<int> transfer[5];
    int count{0};
    int count_0{0}, count_1{0};
    
    read.open("Day_3_Input_test.txt");
    std::cout << "Opening Input File!" << std::endl;
    if(!read.is_open()){
    std::cerr << "Failed to open Input File!!! Please check the File name/path and try again!" << std::endl;
    return 1;
    }

    std::cout << "Input File was successfully opened! Beginning analysis." << std::endl;
    while(std::getline(read, line)){
        bits.push_back(line);
    }

    std::cout << std::endl;
    int num{0};
    
    for(int i{0}; i < 5; i++){
    for(int j{0}; j < bits.size(); j++){
        vint[i].emplace_back(1, std::stoi(bits[j].at(0)));
        std::cout << num << " ";
    }
    std::cout << std::endl;
    }
    read.close();
    return 0;
}

Find elsewhere
Top answer
1 of 14
253

There is strtol which is better IMO. Also I have taken a liking in strtonum, so use it if you have it (but remember it's not portable):

long long
     strtonum(const char *nptr, long long minval, long long maxval,
     const char **errstr);

You might also be interested in strtoumax and strtoimax which are standard functions in C99. For example you could say:

uintmax_t num = strtoumax(s, NULL, 10);
if (num == UINTMAX_MAX && errno == ERANGE)
    /* Could not convert. */

Anyway, stay away from atoi:

The call atoi(str) shall be equivalent to:

(int) strtol(str, (char **)NULL, 10)

except that the handling of errors may differ. If the value cannot be represented, the behavior is undefined.

2 of 14
46

Robust C89 strtol-based solution

With:

  • no undefined behavior (as could be had with the atoi family)
  • a stricter definition of integer than strtol (e.g. no leading whitespace nor trailing trash chars)
  • classification of the error case (e.g. to give useful error messages to users)
  • a "testsuite"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

typedef enum {
    STR2INT_SUCCESS,
    STR2INT_OVERFLOW,
    STR2INT_UNDERFLOW,
    STR2INT_INCONVERTIBLE
} str2int_errno;

/* Convert string s to int out.
 *
 * @param[out] out The converted int. Cannot be NULL.
 *
 * @param[in] s Input string to be converted.
 *
 *     The format is the same as strtol,
 *     except that the following are inconvertible:
 *
 *     - empty string
 *     - leading whitespace
 *     - any trailing characters that are not part of the number
 *
 *     Cannot be NULL.
 *
 * @param[in] base Base to interpret string in. Same range as strtol (2 to 36).
 *
 * @return Indicates if the operation succeeded, or why it failed.
 */
str2int_errno str2int(int *out, char *s, int base) {
    char *end;
    if (s[0] == '\0' || isspace((unsigned char) s[0]))
        return STR2INT_INCONVERTIBLE;
    errno = 0;
    long l = strtol(s, &end, base);
    /* Both checks are needed because INT_MAX == LONG_MAX is possible. */
    if (l > INT_MAX || (errno == ERANGE && l == LONG_MAX))
        return STR2INT_OVERFLOW;
    if (l < INT_MIN || (errno == ERANGE && l == LONG_MIN))
        return STR2INT_UNDERFLOW;
    if (*end != '\0')
        return STR2INT_INCONVERTIBLE;
    *out = l;
    return STR2INT_SUCCESS;
}

int main(void) {
    int i;
    /* Lazy to calculate this size properly. */
    char s[256];

    /* Simple case. */
    assert(str2int(&i, "11", 10) == STR2INT_SUCCESS);
    assert(i == 11);

    /* Negative number . */
    assert(str2int(&i, "-11", 10) == STR2INT_SUCCESS);
    assert(i == -11);

    /* Different base. */
    assert(str2int(&i, "11", 16) == STR2INT_SUCCESS);
    assert(i == 17);

    /* 0 */
    assert(str2int(&i, "0", 10) == STR2INT_SUCCESS);
    assert(i == 0);

    /* INT_MAX. */
    sprintf(s, "%d", INT_MAX);
    assert(str2int(&i, s, 10) == STR2INT_SUCCESS);
    assert(i == INT_MAX);

    /* INT_MIN. */
    sprintf(s, "%d", INT_MIN);
    assert(str2int(&i, s, 10) == STR2INT_SUCCESS);
    assert(i == INT_MIN);

    /* Leading and trailing space. */
    assert(str2int(&i, " 1", 10) == STR2INT_INCONVERTIBLE);
    assert(str2int(&i, "1 ", 10) == STR2INT_INCONVERTIBLE);

    /* Trash characters. */
    assert(str2int(&i, "a10", 10) == STR2INT_INCONVERTIBLE);
    assert(str2int(&i, "10a", 10) == STR2INT_INCONVERTIBLE);

    /* int overflow.
     *
     * `if` needed to avoid undefined behaviour
     * on `INT_MAX + 1` if INT_MAX == LONG_MAX.
     */
    if (INT_MAX < LONG_MAX) {
        sprintf(s, "%ld", (long int)INT_MAX + 1L);
        assert(str2int(&i, s, 10) == STR2INT_OVERFLOW);
    }

    /* int underflow */
    if (LONG_MIN < INT_MIN) {
        sprintf(s, "%ld", (long int)INT_MIN - 1L);
        assert(str2int(&i, s, 10) == STR2INT_UNDERFLOW);
    }

    /* long overflow */
    sprintf(s, "%ld0", LONG_MAX);
    assert(str2int(&i, s, 10) == STR2INT_OVERFLOW);

    /* long underflow */
    sprintf(s, "%ld0", LONG_MIN);
    assert(str2int(&i, s, 10) == STR2INT_UNDERFLOW);

    return EXIT_SUCCESS;
}

GitHub upstream.

Based on: https://stackoverflow.com/a/6154614/895245

🌐
Arduino Forum
forum.arduino.cc › international › deutsch
String zu Int - Deutsch - Arduino Forum
March 5, 2012 - Komme dabei allerdings nicht weiter. Kann mir jemand einen Denkanstoß / Befehl geben? Möglicher Code String Zahl="101"; int Nummer = parseInt(Zahl); //könnte so aussehen , funktioniert aber nicht.
🌐
Udemy
blog.udemy.com › home › it & development › software development › c string to int: simple ways to convert to numeric values
C String to Int: Simple Ways to Convert to Numeric Values - Udemy Blog
April 14, 2026 - The first method is to manually convert the string into an integer with custom code. The second method is to use the atoi function included with the C standard library.
🌐
SysAdminSage
sysadminsage.com › home › blog › convert string to int c: full guide
Convert String to Int C: Full Guide - SysAdminSage
August 21, 2026 - The atoi() function is perhaps the simplest function for this task. If you require a quick, basic conversion and are not concerned with validating the input data, atoi() is straightforward. It takes a C-style string (const char*) and attempts to convert the beginning of the string into an integer ...
🌐
elmah.io
blog.elmah.io › csharp-how-to-convert-a-string-to-int
How to convert a string to int with C# - The extended story
July 21, 2026 - Compares three ways to convert a string to int in C#: int.Parse (throws FormatException on bad input), int.TryParse (returns a bool with no exception needed, the recommended approach), and Convert.ToInt32, whose char overload silently converts the character 1 to its UTF code 49, not the number 1.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-convert-an-integer-to-a-string-in-c
How to Convert an Integer to a String in C? - GeeksforGeeks
July 23, 2025 - In C, integers can be represented as strings with each digit represented by corresponding numeric character.
🌐
ByteHide
bytehide.com › blog › turning a string into an int in c#: full guide
Turning a String into an Int in C#: Full Guide - ByteHide
March 27, 2025 - In C#, ‘String’ and ‘Int’ are data types. A ‘String’ usually contains words and phrases (like “Hello World!”), while ‘Int’ stands for integer, which is a number without a fractional part (like 42.00). So when we say “convert string to int,” we’re talking about taking a number that’s been formatted as a string (like “42”) and converting it back into an integer (like 42).