Hacker News new | ask | show | jobs
by benj111 17 days ago
How do you represent a leading 0?

I don't know enough about phone numbers to say whether you could encode them as an int anyway. In the UK all standard numbers start 0 and international codes start 00? Of course you could introduce a 'locale' and then 20 years later try to solve that mess with multiple ways of representing international numbers. Or maybe not teach young programmers that you can make the phone number a numeric field?

So it seems to me the solution is to save them as BCD...

3 comments

In addition to digits, conventions for phone numbers include the plus sign (international prefix), comma (dial pause), dashes or parentheses (zone/area codes), and spaces (logical separation). I don't see why you'd want to burden yourself with having to regenerate a locally-correct phone number representation each time it needs to be displayed to the user.
I think you and and your parent are in agreement, you CAN do something, but it's not useful, and then you have to either by convention or additional fields deal with the leading zeros and other non-numeric aspects of phone numbers.

I would also teach young programmers (about data) with the shorthand used above, things like "Do you want to add phone numbers? Multiply them? Utilize fractional components? Why are we storing these as numbers?"

I'm not sure we are. My parent suggested you could, in the sense of why would you. I'm questioning if it's possible at all. And if so, is that just based on a local solution. Ie all numbers have an implicit 0 at the start.

To me storing as a number to save space is a fine goal itself. The issue is the underlying complexity which a bare int can't capture. Suggesting using an int indicates the proposer hasn't thought about the issue.

This isn't to say the parent was wrong person se. Asking why you are doing something is a good question. But also understanding the issue/complexity is important too.

In the example I would say that saving space is a sufficient reason, so the point is moot. The reason why you wouldn't want to do it is because a phone number is not 'just a number' (I'm sure there's a mathematical term for a number where leading zeros are important etc).

> I'm questioning if it's possible

It’s certainly possible, but you’ll regret it. Deeply.

Recognising terrible ideas is an essential skill for software engineers.

So the UK starts phone numbers with a 0 generally. International numbers 00 (or plus)

You can't represent a leading zero and because there's 2 for an international code you either rely on the 2 not conflicting. So in the UK's case, the international code is 0044 you would have to rely on no other countries using 044 for domestic numbers.

You could get around that by using the sign bit so +44 is literally a positive integer and a non international prefix is negative?

Then there's the non standard phone numbers, eg emergency services. How do they play with all this. Does 0999 or 0911 work? Does that work internationally?

The biggest issue that I can't see around is that Australia's emergency number is 000. How do you encode that as an integer? How do you differentiate from the old US operator number of 0?

The problem is a leading 0 isn't nothing in a phone number. But it is in an integer on a computer.

So then we're left with the definition of possible. Maybe you can encode all the numbers in your particular region. That's possible. But you can't encode any legal phone number globally.

“But why would you need to keep phones from other countries? All your clients will always be under your own area code.”

I used to make comments like that to highlight the absurdity of all this. I have a Leslie Nielsen level of deadpan delivery.

I read it as a Socraticish anti-question - answering it will answer why its a bad idea.
The use of "What could possibly go wrong" as a provocation, to make students explore what actually can go wrong when we provide a terrible idea (or a good one that has a couple terrible aspects hidden inside them)
Encode every 0 as 10 - then decode every 10 back to 0. Or more efficiently, the first 0 as 10, then decode the the first 10 as 0.
Add 10 to every digit, then multiply it by 10. Concatenate the numbers into the integer you will store.

I love to play absurd games.

That's not very efficient