I've always wondered why leading zeroes (0
) are used to represent octal numbers, instead of — for example — 0o
. The use of 0o
would be just as helpful, but would not cause as many problems as leading 0
es (e.g. parseInt('08');
in JavaScript). What are the reason(s) behind this design choice?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
All modern languages import this convention from C, which imported it from B, which imported it from BCPL.
Except BCPL used #1234
for octal and #x1234
for hexadecimal. B has departed from this convention because # was an unary operator in B (integer to floating point conversion), so #1234 could not be used, and # as a base indicator was replaced with 0.
The designers of B tried to make the syntax very compact. I guess this is the reason they did not use a two-character prefix.
回答2:
"0b" is often used for binary rather than for octal. The leading "0" is, I suspect for "O -ctal".
If you know you are going to be parsing octal then use parseInt('08', 10);
to make it treat the number as base ten.