Jump to content

4 UDFs - dec2bin, bin2dec, power (Exponent), converter (just displays numbers with commas)


Adam1213
 Share

Recommended Posts

I was shocked to find out that there was not a UDF already to do this.

The Bin2Dec algorithm for converting is from Wikipedia

1101

3 2 1 0

= (1 × 2 ) + (1 × 2) + (0 × 2) + (1 × 2)

= (1 × 8) + (1 × 4) + (0 × 2) + (1 × 1)

= 8 + 4 + 2 + 1

= 13 decimal

Dec2bin

Converting 20 (dec) to binary - step 1

20/2 = 10 (+ remainder 0)

10/2 = 5 (+ remainder 0)

5/2 = 2 (+ remainder 1)

2/2 = 1 (+ remainder 0)

1/2 = 0 (+ remainder 1)

$numin=128; start value

$mode=1; 0 bin2dec, 1 dec2bin

if $mode then 
    $type3="dec2bin"
    $type="decimal"
    $type2="binary"
    $notbin=1; if false than the input will be valadated to make sure that it is only 1s and 0s 
else
    $type3="bin2dec"
    $type="binary"
    $type2="decimal"
    $notbin=0
endif

;==================== ASK FOR VALUE TO CONVERT AND VALADATE IT =====================
$pass=0
do
    $numin=inputbox("Decimal + Binary converter", "Enter a " & $type & " number to convert to " & $type2,$numin)
    if @error then exit
    If $numin = "" Then ContinueLoop
    $test=StringRegExpReplace($numin, "[01]", "")
    if $test="" then
        $pass=1
        if $mode=3 then
            $type3="bin2dec"
            $type="binary"
            $type2="decimal"
        endif
    else
        if NOT $notbin then msgbox(0,"Error", """" & $numin & """ is not binary, only 0s and 1s are accepted")
    endif
until $pass OR  $notbin

;==================== CALL CONVERT FUNCTION AND DISPLAY RESULT =====================

$ans=bin2dec($numin)

if $mode then 
    $ans=dec2bin($numin)
else
    $ans=bin2dec($numin)
endif

if $mode then 
    $adec=$numin
    $abin=$ans
else
    $adec=$ans
    $abin=$numin
endif

msgbox(0,"Result - " &  $type3, _
    "Decimal: " & $adec & " - " & convert($adec) & @CRLF & _
    "Binary  : " & $abin  & " - " & convert($abin))


;==================== BINARY TO DECIMAL =====================
func bin2dec($no,$d=0)
    $slen=stringlen($no)
    global $bin=0
    for $t=1 to $slen step 1
        $dec=stringmid($no, $t, 1)
        $a=$dec*_onpower(2,$slen-$t)
        $bin=$bin + $a
        if $d then msgbox(0,"",  _
         "Binary: " & $no & @CRLF & _
        "At: " & $dec & " - Place: " & $t-1 & " - Multiply " & $slen-$t & " - Added: " & $a & @CRLF & _
        "Current value: " & $bin)
    next
    Return ($bin)
endfunc

;==================== DECIMAL TO BINARY =====================

func dec2bin($no,$d=0)

    $str=""
    for $t=0 to $no+1 step 1
        $no/=2
        $temp=stringinstr($no,".")
        if NOT $temp=0 then
            $no=stringleft($no,$temp-1)
            $str="1" & $str
        else
            $str="0" & $str
        endif
    
        if $d then msgbox(0,"Info", _
        "Number is now: """ & $no & """" & @CRLF & _
        "String is now   : """ & $str & """")

        if $no<=0 then exitloop
    next


    Return ($str)
endfunc

;==================== POWER OF  ===============================

func _onpower($no1,$times=2); user onpower(Number, [Power]) Default for power 2
    $no2=1
    for $t=1 to $times step 1
        $no2*=$no1
;msgbox(0,"Info", "At: " & $t & " Value: " & $no2 & " Multiply: " & $no1)
    next
    if $times=0 then $no2=1
    Return ($no2)
endfunc

; ===================== CONVERT =================================

func convert($ans)
    $anst=""
    for $t=stringlen($ans)-2 to -1 step -3
        $added=stringmid($ans, $t, 3) & ","
        $anst=$added & $anst
    next
    $ans=stringtrimright($anst,1)
    Return ($ans)
endfunc
Edited by Adam1213
Link to comment
Share on other sites

I was shocked to find out that there was not a UDF already to do this.

If you check the forum section titles then you may realize another shock that members post in the Scripts'n'Scraps forum for sharing UDFs.

A UDF thread link below has these functions.

http://www.autoitscript.com/forum/index.ph...4791&hl=Bin2Dec

I believe if you search, then a few other converter functions will be found. Posting and searching in the correct forum section may reap good results for almost anything of interest.

Nice effort even though.

Link to comment
Share on other sites

Also if you would have searched for BinToDec you would have found a recently created UDF from myself.

;===============================================================================
;
; Function Name:    _BinToDec("binary")
; Description:    Converts a binary number to a decimal number.
; Parameter(s):  $i_Binary is the binary number to be converted.
; Requirement(s):   Binary Input.
; Return Value(s):  On Success - Returns the converted binary.
;                  On Failure - -1  and sets @ERROR = 1
; Author(s):        Jarvis Stubblefield
;
;===============================================================================

Func _BinToDec($i_Binary)
    Local $i_Array, $i_Decimal
    
    If IsInt($i_Binary) Then
        $i_Array = StringSplit($i_Binary, "")
        
        For $i = $i_Array[0] To 1 Step -1
            $i_temp = $i_Array[$i]*2^($i_Array[0] - $i)
            $i_Decimal = $i_Decimal + $i_temp
        Next
        
        Return $i_Decimal
    Else
        Return -1
        SetError(1)
    EndIf
EndFunc

Let me know if you are going to submit your UDF's to the UDF Standard Library. Also as MHz said... this is a Scripts and Scraps post.

I havent made the DecToBin to go with it yet.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...