THE PUZZLET PAGE


How_Does_It_Work


FUNCTION:  Dec2Oct().  This useful function takes a denary integer as its argument (a number in base 10 without a decimal part) and converts it into the octal form (base 8).  The returned octal number is in string format.

This code will only do conversions up to the maximum possible value of its argument. So, when declaring Dec2Oct(), its arguments must be declared as type integer (int). See the table below for the limits.

Type

Exponent of 2
Value

int 230 1073741824
uint 231 2147483648
int64 261 2305843009213693952
uint64 262 4611686018427387904


Declaring the function must be done like this:

declare Dec2Oct(d: int) (or whatever type of integer you choose).

Here's the code:


sub Den2Oct(d: int)
' converts denary argument d into octal
' and returns the result in string format.
def oct$: string
oct$ = ""
do
    oct$ = ltrim$(str$(d % 8)) + oct$
    d = d/8
until d = 0
return oct$


Overview:  the algorithm employed is the classic one used for this function. The argument is successively divided by eight until it is reduced to zero. The remainders are saved in string format at each division.  When the argument reaches zero, the octal form is already in the string, ready to be returned to the calling routine.

Here's an example finding the octal form of 7769 in denary.

Denary
Denary/8
Remainder
Saved String
7769
971
1
"1"
971
121
3
"31"
121
15
1
"131"
15
1
7
"7131"
1
0
1
"17131"

Remember, the saved string was empty at the beginning.  At each iteration, the remainder is converted to string format and the last value of the saved string, oct$, tacked onto the end.  You can see that the process is terminated as soon as Denary/8 equals zero (regardless of any remainder).

In order to simplify a line-by-line description of the code I've numbered each line, as shown below..


1
2
3
4
5
6
7

def oct$: string
oct$ = ""
do
    oct$ = ltrim$(str$(d % 8)) + oct$
    d = d/8
until d = 0
return oct$

Let's look at the above code in detail now.

1

def oct$: string.  Creates the variable that will be used to accumulate the result in.

2

oct$ = "".  Initialises oct$ to make sure it's empty to begin with.  This is just good housekeeping.

3

do.  Opens the main DO loop.

4

oct$ = ltrim$(str$(d % 8)) + oct$.  This is the line that does all the work!  Let's work from the inside out.

Expression
Explanation
d % 8 This is integer arithmetic. It divides d by 8 and retains only the remainder.
str$(d % 8) Converts the remainder of dividing d by 8 into string format.
ltrim$(str$(d % 8) The built-in str$ function tacks a space onto the front of its argument.  ltrim$ deletes this unwanted space.
ltrim$(str$(d % 8) + oct$ Tacks whatever was already in oct$ onto the end of the newly-created string

So line 4 can be paraphrased as saying "Find the remainder of dividing argument d by 8 and convert it into string format; make a new string by tacking whatever was already stored in variable oct$ onto the end; save this new string into variable oct$, over-writing whatever was already in there."

Note that the original value in variable d is unchanged by this action.  All we've done is worked out wat the remainder of dividing argument d by 8 would be.


5

d = d/8.  Having successfully up-dated oct$, the code is now ready to alter the contents of argument d.  It has to be divided by 8.  The conversion process at this point requires that any remainder is discarded, since it was already dealt with in line 4. 

Argument d was declared to be an integer in the declarations, and this now works to our advantage.  Because it's an integer, any remainder is automatically discarded for us.  For example, if argument d holds value 19, dividing by 8 would result in 2, not 2375.

6

until d = 0.  We've reached the end of the loop, and it's time to make a decision: execute it again, or move on to the rest of the code?  This process works by repeating until the last division of the argument by 8 returns zero.  So the code just looks for value zero.  Line 6 is saying "If the last DO-LOOP process didn't reduce d to zero, execute it again, otherwise quit the loop."

7

return oct$.  Variable oct$ now holds the octal version of the original argument d.  All that remains now is to return it to the calling routine, and line 7 does that for us.


MAIN MENU
HOW DOES IT WORK?

Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 4th, 2010.