kstrtoul(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’– 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: if base is 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
  • res β€” where to write the result of the conversion on success. That's the drop spot. 🫢

πŸ”₯ DESCRIPTION πŸ”₯

So the returns, bestie, listen up:

  • 0 on success πŸŽ‰
  • -ERANGE on overflow πŸ’€
  • -EINVAL on 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