How the C programming language bridges the man-machine gap

Doug Ewell doug at ewellic.org
Fri Apr 15 11:20:49 CDT 2022


Roger L Costello wrote:

> (As you know) Every character is represented inside the computer as a
> number. To convert the character 1 to the number 1 we subtract the
> numeric representation of the character 1 from the numeric
> representation of the character 0:
>
> int val = ch - '0';
>
> To convert the sequence of characters 123 to the number 123 we iterate
> through each character, converting the character to a number, and then
> multiplying by 1 or 10 or 100, etc. depending on the position in the
> string:
>
> while ( ch = getchar() )
>     val = val * 10 + ch - '0';
>
> That little loop bridges the gap between the human-friendly string 123
> and the machine-friendly number 123. Neat!

It seems you had already discovered this about sixteen months earlier:

https://corp.unicode.org/pipermail/unicode/2020-December/009200.html

The Unicode list is just about the last place you want to promote these ASCII-constrained code snippets. There are plenty of examples of integer-to-string and string-to-integer conversion functions that are much more Unicode-aware. In fact, Frédéric Grosshans addressed exactly that in his posts dated 2020-12-16 on the thread mentioned above.

--
Doug Ewell, CC, ALB | Lakewood, CO, US | ewellic.org





More information about the Unicode mailing list