Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilpropheta/ec12b8dccae93132aede78f73ffb90e8 to your computer and use it in GitHub Desktop.
Save ilpropheta/ec12b8dccae93132aede78f73ffb90e8 to your computer and use it in GitHub Desktop.
int to_int(string_view sv)
{
char formatter[24] = {};
sprintf(formatter, "%%%zud%%n", sv.size());
int num, n{0};
if (sscanf_s(sv.data(), formatter, &num, &n)==1 && n==sv.size()) // could emit a warning because sv.size() is unsigned
return num;
else // (some) error handling
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment