Page 1 of 1 1
Topic Options
#50824 - 2000-07-31 10:57 PM binary to decimal and reverse convertion
Anonymous
Unregistered



Does any one knows if there is any ready script for decimal to binary and reverse convertion ? I need it for some subnet calculations.
Thanks

Top
#50825 - 2000-08-01 10:25 AM Re: binary to decimal and reverse convertion
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
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).]

Top
#50826 - 2000-08-02 02:32 PM Re: binary to decimal and reverse convertion
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
NovaStar,

subnet calculation?
ever heard of this one?:
http://www.quadritek.com/Products/abtsubc.html

just one of the bazillions available...

Jochen

_________________________



Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 384 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.052 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org