<sect1><title>Octal</title>
<sect2><title>Definition</title>
<para>
Octal is a counting system based on eight digits (0,1, 2, 3, 4, 5, 6, 7). To expand an octal number to decimal, multiply each digit in the number with the corresponding power of eight. The following shell script demonstrates this:
</para>

<programlisting>
#!/bin/bash

# Take an octal number and output it's decimal version
octal=$1
length=`echo $octal | wc -c | tr -d " "`
power=1

while [ $length -gt 2 ]
do
  power=$(( $power * 8 ))
  length=$(( $length - 1 ))
done

decimal=0
while [ "%$octal%" != "%%" ]
do
  digit=`echo $octal | cut -b 1`
  echo -n "Octal = $octal, Digit = $digit, Power = $power, Value = "

  value=$(( $digit * $power ))
  echo $value
  
  decimal=$(( $decimal + value ))
  power=$(( $power / 8 ))
  octal=`echo $octal | sed 's/^.//'`
done

echo ""
echo "Result = $decimal"
</programlisting>

<para>
The following shell script example shows how to convert a decimal number to octal from first principles:
</para>

<programlisting>
#!/bin/bash

# Take a decimal number and output it's octal
quotient=$1
while [ $quotient -ne 0 ]
do
  remainder=$(( $quotient % 8 ))
  octal="$remainder$octal"
  quotient=$(( $quotient / 8 ))
done

echo $octal
</programlisting>
</sect2>

<sect2><title>References</title>
<itemizedlist>
<listitem><para><emphasis>Computer Engineering Hardware Design</emphasis> by <emphasis>M Morris Mano</emphasis> published by Prentice Hall 1988</para></listitem>
</itemizedlist>
</sect2>

<sect2><title>See also</title>
<itemizedlist>
<listitem><para><emphasis>Binary</emphasis></para></listitem>
<listitem><para><emphasis>Decimal</emphasis></para></listitem>
<listitem><para><emphasis>Hexadecimal</emphasis></para></listitem>
</itemizedlist>
</sect2>

<sect2><title>Entry history</title>
<itemizedlist>
<listitem><para>Entry created: Thu Mar 20 15:20:12 EST 2003</para></listitem>
<listitem><para>Entry owner: <emphasis>Michael Still</emphasis> (mikal@stillhq.com)</para></listitem>
<listitem><para>Status: Finalized</para></listitem>
</itemizedlist>
</sect1>