Jump to content

Decimal to any base


Joke758
 Share

Recommended Posts

With this UDF, you can convert a decimal number to any base (binary, oct, hex, base32, base64) and you can even create your own base.

#include <Array.au3>

$base64 = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,"& _
"j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,+,/"
$base32 =  "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,2,3,4,5,6,7"
$base16 =  "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F"
$base8 = "0,1,2,3,4,5,6,7"
$base2 =  "0,1"

$number = InputBox ( "Decimal Converter", "Enter a decimal number", "", "", -1, 100, -1, -1 )
$bin = _Base ( $number, $base2 )
$oct = _Base ( $number, $base8 )
$hex = _Base ( $number, $base16 )
$32 = _Base ( $number, $base32 )
$64 = _Base ( $number, $base64 )

$text = "Binary: "&$bin&" -> Dec: "& _Dec($bin, $base2)&@CRLF&@CRLF
$text &= "Oct: "&$oct&" -> Dec: "& _Dec($oct, $base8)&@CRLF&@CRLF
$text &= "Hex: "&$hex&" -> Dec: "& _Dec($hex, $base16)&@CRLF&@CRLF
$text &= "Base 32: "&$32&" -> Dec: "& _Dec($32, $base32)&@CRLF&@CRLF
$text &= "Base 64: "&$64&" -> Dec: "& _Dec($64, $base64)

Msgbox ( 0, "Decimal Converter", $text )


;===============================================================================
;
; Function Name:   _Base
; Description::    Convert a decimal number to any base
; Parameter(s):    $iDecNumber - The decimal number to convert
;                  $sChar - Characters separated by commas
; Requirement(s):  <Array.au3>
; Return Value(s): The converted number in the specified base.
; Author(s):       Joke758
; Note(s):         $sChar should look like: "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F"
;                  
;===============================================================================

Func _Base($iDecNumber, $sChar)
    Local $aBase = StringSplit ( $sChar, "," )
    Local $iBase = $aBase[0]
    Local $iLen = Int ( Log($iDecNumber) / Log($iBase) )+1
    Dim $iUnit[$iLen+1]
    Local $iReturn
    Local $iTemp = $iDecNumber
    
    If $aBase[0] <> $iBase Then
        SetError (1)
        Return -1
    EndIf
    
    If $iDecNumber = 0 Then Return $aBase[1]
    
    For $i = 1 to $iLen
        For $i2 = 1 to $i-1
            $iTemp = $iTemp/$iBase
        Next
        $iUnit[$i] = $aBase[Mod ( $iTemp, $iBase )+1]
        $iTemp = $iDecNumber
    Next
    
    For $i = 1 to $iLen
        $iReturn = $iUnit[$i] & $iReturn
    Next

    Return $iReturn
EndFunc

;===============================================================================
;
; Function Name:   _Dec
; Description::    Convert a x number in a decimal number
; Parameter(s):    $iNumber - The number to convert
;                  $sChar - Characters separated by commas
; Requirement(s):  <Array.au3>
; Return Value(s): A decimal number
; Author(s):       Joke758
; Note(s):         $sChar should look like: "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F"
;                  
;===============================================================================

Func _Dec ( $iNumber, $sChar )
    Local $iReturn
    Local $aBase = StringSplit ( $sChar, "," )
    Local $iBase = $aBase[0]
    $iLen = StringLen ( $iNumber )
    
    For $i = 0 to $iLen-1
        $iTemp = StringMid ( $iNumber, $iLen-$i, 1 )
        $iReturn = $iReturn + ($iBase^$i)*(_ArraySearch ( $aBase, $iTemp )-1)
    Next
    
    Return $iReturn
EndFunc

[u]My Programs:[/u]Word Search Creator - Make your own Word SearchShortHand - Hide a secret message in a jpg fileHex Editor - Edit your Binary fileIncrease file size - Increase the size of any filesArt Generator - A program that generate random picture[u]My Functions:[/u]16-Bits Hash - My Hash function similar to MD5Probabilities - My probabilities function (factorial, permuation, combination)_GetDate() - Convert a date to a day of the week_Base(), _Dec() - Convert a number in any base (bin, oct, hex, dec). Create your own!

Link to comment
Share on other sites

  • 1 year later...

Func _Dec ( $iNumber, $sChar )
    Local $iReturn
    Local $aBase = StringSplit ( $sChar, "," )
    Local $iBase = $aBase[0]
    $iLen = StringLen ( $iNumber )
    
    For $i = 0 to $iLen-1
        $iTemp = StringMid ( $iNumber, $iLen-$i, 1 )
    ;   $iReturn = $iReturn + ($iBase^$i)*(_ArraySearch ( $aBase, $iTemp )-1) ;      <<< change this line<<<
        $iReturn = $iReturn + ($iBase^$i)*(_ArraySearch ( $aBase, $iTemp,0,0,1 )-1) ; <<< to this
    Next
    
    Return $iReturn
EndFunc
make it case sensitive,

Base64 or the like wont work,

thanks for this code, I was coding my own when I found yours =))

( to make it easier to find here are some strings: BaseTo Base2 Dec2Base DecToBase )

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Joke758

I liked the idea of being able to enter any base number system as a string. So I added Base 12 and Base 60.

Base 60 being used in degrees, minutes, seconds or hours, minutes, seconds.

Then had to modify your _Dec() function for conversion of base 60 back to decimal number system.(base 10), because of the three characters per number used in the base 60 string.

I then realized it did not matter which characters were used in the base string. As long as there is equal number of characters between the commas. And, the character used in the base string also appears in the StringRegExp() function in the modified _Dec() function to be able to convert it back to base 10.

Thus, the code base string manifested.

Malkey.

#include <Array.au3>

$base64 = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i," & _
          "j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,+,/"
;Sexagesimal
$base60 = "00:,01:,02:,03:,04:,05:,06:,07:,08:,09:,10:,11:,12:,13:,14:,15:,16:,17:,18:,19:," & _
          "20:,21:,22:,23:,24:,25:,26:,27:,28:,29:,30:,31:,32:,33:,34:,35:,36:,37:,38:,39:," & _
          "40:,41:,42:,43:,44:,45:,46:,47:,48:,49:,50:,51:,52:,53:,54:,55:,56:,57:,58:,59:"
$base32 = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,2,3,4,5,6,7"
$base16 = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F"
;Code
$base13 = "As,/y,qu,ro,Am,1D,Wo,0H,HO,2A,AM,0x,A2"
;Duodecimal system or dozenal
$base12 = "0,1,2,3,4,5,6,7,8,9,A,B"
$base8 = "0,1,2,3,4,5,6,7"
$base2 = "0,1"

$number = InputBox ( "Decimal Converter", "Enter a decimal number", "", "", -1, 100, -1, -1 )
$bin = _Base($number, $base2)
$oct = _Base($number, $base8)
$Duod = _Base($number, $base12)
$Code = _Base($number, $base13)
$hex = _Base($number, $base16)
$32 = _Base($number, $base32)
$Sexa1 = _Base($number, $base60)
$64 = _Base($number, $base64)

$text = "Binary       : " & $bin & " -> Dec: " & _Dec($bin, $base2) & @CRLF & @CRLF
$text &= "Oct         : " & $oct & " -> Dec: " & _Dec($oct, $base8) & @CRLF & @CRLF
$text &= "Duodecimal  : " & $Duod & " -> Dec: " & _Dec($Duod, $base12) & @CRLF & @CRLF
$text &= "Code Base13 : " & $Code & " -> Dec: " & _Dec($Code, $base13) & @CRLF & @CRLF
$text &= "Hex         : " & $hex & " -> Dec: " & _Dec($hex, $base16) & @CRLF & @CRLF
$text &= "Base 32     : " & $32 & " -> Dec: " & _Dec($32, $base32) & @CRLF & @CRLF
$text &= "Sexagesimal : " & StringTrimRight($Sexa1, 1) & " -> Dec: " & _Dec($Sexa1, $base60) & @CRLF & @CRLF
$text &= "Base 64     : " & $64 & " -> Dec: " & _Dec($64, $base64)

MsgBox(0, "Decimal Converter", $text)


;===============================================================================
;
; Function Name:   _Base
; Description::    Convert a decimal number to any base
; Parameter(s):    $iDecNumber - The decimal number to convert
;                  $sChar - Characters separated by commas
; Requirement(s):  <Array.au3>
; Return Value(s): The converted number in the specified base.
; Author(s):       Joke758
; Note(s):         $sChar should look like: "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F" 0r
;                  "00:,01:,02:,03:,04:, ...". The same number of characters between comas.
;
;===============================================================================

Func _Base($iDecNumber, $sChar)
    Local $aBase = StringSplit($sChar, ",")
    Local $iBase = $aBase[0]

    Local $iLen = Int(Log($iDecNumber) / Log($iBase)) + 1

    Dim $iUnit[$iLen + 1]
    Local $iReturn
    Local $iTemp = $iDecNumber

    If $aBase[0] <> $iBase Then
        SetError(1)
        Return -1
    EndIf

    If $iDecNumber = 0 Then Return $aBase[1]

    For $i = 1 To $iLen
        For $i2 = 1 To $i - 1
            $iTemp = $iTemp / $iBase
        Next
        $iUnit[$i] = $aBase[Mod($iTemp, $iBase) + 1]
        $iTemp = $iDecNumber
    Next

    For $i = 1 To $iLen
        $iReturn = $iUnit[$i] & $iReturn
    Next

    Return $iReturn
EndFunc   ;==>_Base

;===============================================================================
;
; Function Name:   _Dec
; Description::    Convert a x number in a decimal number
; Parameter(s):    $iNumber - The number to convert
;                  $sChar - Characters separated by commas
; Requirement(s):  <Array.au3>
; Return Value(s): A decimal number
; Author(s):       Joke758
; Modified :       Malkey
; Note(s):         $sChar should look like: "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F" 0r
;                  "00:,01:,02:,03:,04:, ...". The same number of characters between comas.
;
;===============================================================================

Func _Dec($iNumber, $sChar)
    Local $iReturn
    Local $aBase = StringSplit($sChar, ",")
    Local $iBase = $aBase[0]
    Local $aTemp = StringRegExp($iNumber, "[a-zA-Z0-9:+/]{" & StringLen($aBase[1]) & "}", 3)
    For $i = 0 To UBound($aTemp) - 1
        $iReturn += ($iBase ^ $i) * (_ArraySearch($aBase, $aTemp[UBound($aTemp) - 1 - $i], 0, 0, 1) - 1)
    Next

    Return $iReturn
EndFunc   ;==>_Dec
Link to comment
Share on other sites

  • 3 months later...
  • 3 years later...

Not to revive an old post, but I am having some trouble in my code, I am able to convert the text to my base104, however I am unsure how to convert it back, it seems to become corrupted when I do so, perhaps I am doing something wrong?

The code to convert:

Global $base104 = "H¦,He,Li,Be,B¦,C¦,N¦,O¦,F¦,Ne,Na,Mg,Al,Si,P¦,S¦,Cl,Ar,K¦,Ca,Sc,Ti,Cr,Mn,Fe,Co,Ni,Cu,Zn,Ga,Ge,As,Se,Br,Kr,Rb,Sr,Y¦,Zr,Nb,Mo,Tc,Ru,Rh,Pd,Ag,Cd,In,Sn,Sb,Te,I¦,Xe,Cs,Ba,La,Ce,Pr,Nd,Pm,Sm,Eu,Gd,Tb,Dy,Ho,Er,Tm,Yb,Lu,Hf,Ta,W¦,Re,Os,Ir,Pt,Au,Hg,Tl,Pb,Bi,Po,At,Rn,Fr,Ra,Ac,Th,Pa,U¦,Np,Pu,Am,Cm,Bk,Cf,Es,Fm,Md,No,Bh,Rg,Fl"
for $i=1 to $text[0]
$number &= _Base(AscW($text[$i]), $base104)
Next

The code to convert it back:

$string = StringSplit($string, "")
$text = ""
for $i=1 to $string[0] step 2
$text &= _dec($string[$i]&$string[$i+1], $base104)
Next

(I do not know what to do with the numbers returned from _dec)

Edited by nullschritt
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...