kstrtoul(9)
π kstrtoul β like, the kstrtoul(9) girlies' field guide π
Okay bestie, so this one is from the Kernel API Manual (that's the section 9 energy, kernel-internals only, iykyk), and it's genuinely one of the most useful little functions ever. It's called kstrtoul and it literally converts a string into an unsigned long. That's it. That's the whole vibe. β¨
β¨ NAME β¨
kstrtoul β convert a string to an unsigned long πΈ
π¦ SYNOPSIS π¦
int kstrtoul (const char *s , unsigned int base , unsigned long *res );
π ARGUMENTS π
sβ the start of the string. And no, it can't just be any string, she has RULES: the string must be null-terminated, and it may also include a single newline before its terminating null. The first character may also be a plus sign, but not a minus sign. Not even a little minus sign. Nope. πbaseβ the number base to use. The maximum supported base is 16. But here's the iconic part: ifbaseis given as 0, then the base of the string is automatically detected with the conventional semantics, like she's reading your mind:- if it begins with
0x, the number will be parsed as a hexadecimal (case insensitive π) - if it otherwise begins with
0, it will be parsed as an octal number - otherwise it will be parsed as a decimal
- if it begins with
resβ where to write the result of the conversion on success. That's the drop spot. π«Ά
π₯ DESCRIPTION π₯
So the returns, bestie, listen up:
0on success π-ERANGEon overflow π-EINVALon parsing error π
She's preferred over simple_strtoul β like, that's the whole point, she's the upgraded main character version. β¨
And the return code must be checked. That is not optional. Not "oh I'll get to it later". Check it. The kernel will not be sending you a friendly little follow-up message, she will just break. π¨
π SEE ALSO π
- Kernel file
./include/linux/kstrtox.h kstrtol(9)
okay that's the whole page, no cap, you got this π§π
Signed off from the ./include/linux corner of the kernel, October 2026 edition β mwah, go check your return codes, ily, byeeee π
ππ
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms