I have my binary/decimal conversion subroutines in a "Library file", so i dont have
to include the code in every script i use them.

Library BinLib.kix:

code:
; ScriptName BinLib.kix
;
; Library of routines for binary conversions
;
; Variables passed allways:
; Input : $bl_CallSub, Subroutine to call
; See remarks in subroutines for other variables passed
; Global $bl_CallSub

Select
Case $bl_CallSub = "Dec2Bin"
GoSub "Dec2Bin"
Case $bl_CallSub = "Bin2Dec"
GoSub "Bin2Dec"
Case 1
? "Error subroutine " + $bl_CallSub + " not found in BinLib.kix"
Get $x
EndSelect

Return

;**************
:Dec2Bin
;**************
;Convert decimal to binary
; Input : $bl_Dec = Decimal value
; Output: $bl_Bin = Converted to binary As string

Global $bl_Bin
Dim $Tmp, $Dig

$bl_Bin = ""
While $bl_Dec > 0
$Tmp = $bl_Dec / 2
$Dig = $bl_Dec - ($Tmp * 2)
$Dig = "" + $Dig ; Convert $Dig from decimal to string
$bl_Bin = $Dig + $bl_Bin
$bl_Dec = $Tmp
Loop

Return


;**************
:Bin2Dec
;**************
;Convert binary to decimal
; Input : $bl_Bin = Binary value As string
; Output: $bl_Dec = Decimal value

Global $bl_Dec
Dim $Fac, $Cif

$Fac = 1
$bl_Dec = 0
While $bl_Bin <> ""
$Cif = Val(SubStr($bl_Bin, Len($bl_Bin), 1))
$bl_Bin = SubStr($bl_Bin, 1, Len($bl_Bin) - 1)
$bl_Dec = $bl_Dec + ($Cif * $Fac)
$Fac = $Fac * 2
Loop

Return



Example of using BinLib.kix:
code:
; Using BinLib.kix
Break On

$bl_Dec = 837
"Input " + $bl_Dec

$bl_CallSub = "Dec2Bin"
Call "BinLib.kix"

?"Bin " + $bl_Bin

$bl_CallSub = "Bin2Dec"
Call "BinLib.kix"

?"Dec " + $bl_Dec
?"Press a key to end "
Get $x

Return



Erik

[This message has been edited by kholm (edited 02 August 2000).]