4 ms·
To the first problem you can use a classic mod 10 divide by 10 loop to convert into a string digit by digit. Similarly for the second problem you can read digi
by orlp 4y ago
To the first problem you can use a classic mod 10 divide by 10 loop to convert into a string digit by digit.
Similarly for the second problem you can read digit by digit in reverse order and create the number using x = 10x + d while detecting overflow.
I'm not saying that this is a great thing to need to do, but the mentioned problems aren't particularly fundamental and the solutions are CS101 stuff.
- teddyh 4y agoYou may be right that it’s the best solution. It’s horrible to contemplate, but it might be so.
- orlp 4y agoOh I agree, it's bad.
- jcranmer 4y agoThe best solution is to say that uid_t et al are probably no larger than uint64_t, and introducing a static_assert that makes sure that the compiler will fail if it's not. Sure, someone might come up with an architecture where that's not true. But even in an intmax_t-is-really-the-largest-integer-type world [which hasn't been true for the major C compilers for the past decade or so!], I can almost assuredly come up with some compilation modes that still conform with whichever C version you use that still causes your code to fail horribly. I don't think there's any architecture where uid_t et al would exceed uint64_t, and until such an architecture comes to exist, it's quite frankly not worth caring about.
- csmpltn 4y ago> "To the first problem you can use a classic mod 10 divide by 10 loop to convert into a string digit by digit." But the "physical" size of the number as represented in memory can change across platforms, no?