An octal value is an integer composed of only the following characters:
0 1 2 3 4 5 6 7
Octal values are always prefixed by a zero character.
The sequence carries over for the next increment when each column reaches the value 7. Thus:
00 01 02 03 04 05 06 07 010 011 012
Octal values have a historical significance from having been used in the earliest computer systems. However these days, they are particularly useful since they map quite conveniently to the binary system. Each octal digit corresponds to three binary digits.
The most significant of the three octal digits does not have a full range since it contains a carry over bit and a three digit octal number actually represents a 9 bit value. However, an 8 bit value can be encoded conveniently if the range is limited to 0377 as a maximum. Hexadecimal values map far more conveniently although they are harder to compute mentally.
Beware when you prefix decimal values with a zero character. You may want to justify a column of figures. If you add leading zero characters to a numeric string, if that string is subsequently parsed back to a numeric value, you may inadvertently export the value as a decimal but import it as an octal value. This can lead to an extremely difficult-to-diagnose fault in your software because the parsers sometimes add some intelligence and will correctly interpret the value as decimal if the characters 8 or 9 are present, but otherwise interpret it as octal notation.
This may be implementation-dependant behavior to some extent.
Be careful that you remove any leading zero characters from the text strings that you plan to convert using the numeric parser. The example shows a simple function for doing this.
<HTML> <HEAD> </HEAD> <BODY> <SCRIPT> myString = "00123"; document.write(stripLeadingZeros(myString)); // Strip leading zero characters off a numeric string function stripLeadingZeros(aString) { return aString.substr(aString.search(/[1-9]/)); } </SCRIPT> </BODY> </HTML>
Prev | Home | Next |
Obsolescent | Up | Off by one errors |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |