Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Octal Conversion Function

Status
Not open for further replies.

dblarch

Technical User
Mar 8, 2004
29
US
Is there a function analogous to ToHex that will convert decimal integers to octal?
 
The below comes from Ed, over on pnews:
(untested)

var
StOct String
LiDig, LiOct LongInt
NuDec Number
endvar


NuDec = 1357.0 ; start with a number (vs LongInt)
NuDec = NuDec / 8 ; because "mod" requires it.

LiOct = LongInt(NuDec)
LiDig = LongInt(NuDec.mod(8))

StOct = string(LiOct)+string(LiDig)


; LiDig is the least significant digit
; LiOct is all other more significant digits.



Tony McGuire
"It's not about having enough time. It's about priorities
 
In that same thread, Ed Nash replied with:

I think you'll need to use an iterative subtraction routine, using the
nearest powers of 8. In Ed's example of 1357 decimal you would do:

1357 - (2 * 8 ^3) ---> |2|
remainder = 333

333 - (5 * 8^2) ---> |5|
remainder = 13

13 - (1 * 8^1) ---> |1|
remainder = 5

5 = 5 * 8^0) ---> |5|


Therefore, the correct octal value of 1357 decimal is 2515.


Tony McGuire
"It's not about having enough time. It's about priorities
 
The *original* Ed replied:

Thanks to Steve I checked my work on larger
(> 2 digit numbers) and now cortrect to:

var
StOct String
LiDig, LiOct LongInt
NuDec, NuOct Number
endvar

NuDec = 1357.0

StOct = String(LongInt(NuDec.mod(8)))
NuDec = NuDec / 8
While NuDec > 1
StOct = String(LongInt(NuDec.mod(8))) +StOct
NuDec = NuDec / 8
endwhile


(StOct is the Octal equiv).


Tony McGuire
"It's not about having enough time. It's about priorities
 
I too was working on the "while" loop. But, your solution is more concise and elegant than mine so yours will be used.

During coding I fell into a trap where my loop was endless. I was using the view() method for more than one variable so it cycled through the message boxes endlessly. How does one break that loop? Ctrl-C, Break, Ctrl-Break didn't work. I finally ended the misery with the Task Manager.

Thanks again,

DblArch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top