qlibs:(scan)
β¨ scan β string to integer conversion β¨
okay so this is the scan page from qlibs (that's the manual name, bestie, it's in the (scan) section if you're keeping score) and honestly? it's giving "tiny functions that do one thing and do it well" π
let's get into it.
πΈ NAME πΈ
scan β string to integer conversion. that's it. that's the whole vibe. turning your little text digits into actual numbers π«Ά
π SYNTAX π
#include "scan.h"
unsigned int scan_8long(const char *src,unsigned long *dest);
unsigned int scan_uint(const char *src,int *dest);
unsigned int scan_long(const char *src,long *dest);
unsigned int scan_ulong(const char *src,unsigned long *dest);
unsigned int scan_xint(const char *src,int *dest);
unsigned int scan_xlong(const char *src,unsigned long *dest);
six functions, one header, all of them hand you back the number of bytes they chewed through. iconic behavior β¨
π DESCRIPTION π
okay so every single one of these cuties takes src, parses whatever number situation is living in there, drops the result in dest, and returns the number of bytes read from src. that return value is your bestie fr β it tells you where the number ended and everything else began π¦
scan_8longβ parses an unsigned long integer in octal ASCII representation fromsrcand writes the result intodest. returns the number of bytes read fromsrc.scan_uintβ parses an unsigned integer in decimal ASCII representation fromsrcand writes the result intodest. returns the number of bytes read fromsrc.scan_ulongβ parses an unsigned long integer in decimal ASCII representation fromsrcand writes the result intodest, and yes, returns the number of bytes read fromsrc. this one has rules, bestie, so listen up:- leading
+or-or space (or literally anything outside of0-9) is not accepted. not even a little. - the libc conventions of
"023"for octal or"0x23"for hexadecimal are not supported. none of that. scan_ulongwill abort the scan if the next character is not a digit, or if it is a digit but adding it to the number would lead to an integer overflow. it's protecting you πscan_ulongreturns the number of characters successfully scanned and processed fromsrc.
- leading
scan_longβ same logic asscan_ulong, but now on a signed long integer in decimal ASCII format, and it does recognize the leading+or-sign. the glow-up π«Άscan_xintβ parses an unsigned integer in hexadecimal ASCII representation fromsrcand writes the result intodest. returns the number of bytes read fromsrc.scan_xlongβ parses an unsigned long integer in hexadecimal ASCII representation fromsrcand writes the result intodest. returns the number of bytes read fromsrc. it understands both upper and lower case letters π but it does not expect or understand a"0x"prefix β so don't bring one, it's not that kind of party π
π§ GENERICS π§
so for the scan functions with decimal input β scan_uint, scan_ulong and scan_int als well scan_ulong β you can use the Generic scan_dnum π« and for scan_xint and scan_xlong there's the Generic scan_xnum.
why do you care? because this helps to avoid cases where the input is given as an alias. and the best part: generics are evaluated at compile time, so they don't impact execution performance at all. free real estate, no cap π₯
π₯ EXAMPLES π₯
here's where it gets good, watch:
scan_ulong("23",&i) => i=23, return 2
scan_ulong("+23",&i) => return 0
scan_ulong("-23",&i) => return 0
scan_ulong(" 23",&i) => return 0
no signs, no spaces. we don't do that here π
scan_ulong("23,42",&i) => i=23, return 2
see how the return value is your map? it stopped at the comma πΊοΈ
scan_ulong("023",&i) => i=23, return 3
not octal. just 23. read all three characters though β
scan_ulong("0x23",&i) => i=0, return 1
it grabbed the 0 and immediately clocked out. 0x23 is not the moment π
scan_ulong("4294967296",&i) => i=429496729, return 9 // 32-bit system
overflow guard kicked in, stopped at 9 characters. she's got boundaries π
π SEE ALSO π
case(3), byte(3) β go say hi to the neighbors π«Ά
and that's the page!! brought to you by the 3 crew of qlibs (scan), so go forth and parse responsibly, bestie ππ
π¦ where this page comes from
- alpine 3.24: fehqlibs-doc 31-r0 Β· Public-Domain
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms