#include
<charconv>
from_chars_result from_chars(const char* first, const char* last, INTEGER& value, int base = 10); // C++17 from_chars_result from_chars(const char* first, const char* last, FLOAT& value, chars_format fmt = chars_format::general);
Konvertiert Zeichenkette in Ganzzahl bzw. Gleitkommazahl.
first | Startposition |
last | Endposition |
value | Ergebniswert |
base | Zahlenbasis |
fmt | Zahlformat |
Rückgabewert: Ergebnis enthält Zeiger hinter das Ende der konvertierten Zeichenkette bzw. Fehlercode.
#include <charconv> #include <iostream> #include <string> #include <system_error> int main() { const size_t SIZE = 100; std::string s(SIZE, '*'); char* buffer = &s[0]; int value = 1234567890; if (auto [end, error] = std::to_chars(buffer, buffer+SIZE, value); error==std::errc()) std::cout << s << " " << (end-buffer) << " Bytes written\n"; else std::cout << buffer << '\n' << std::string(end-buffer, ' ') << "^ error: " << std::make_error_code(error).message() << '\n'; value = 0; if (auto [end, error] = std::from_chars(buffer, buffer+SIZE, value); error==std::errc()) std::cout << value << " " << (end-buffer) << " Bytes read\n"; else std::cout << buffer << '\n' << std::string(end-buffer, ' ') << "^ error: " << std::make_error_code(error).message() << '\n'; } }