Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Decimal to any base


  • Please log in to reply
7 replies to this topic

#1 Joke758

Joke758

    Wayfarer

  • Active Members
  • Pip
  • 84 posts

Posted 06 January 2007 - 11:21 PM

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.

AutoIt         
#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

My Programs: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 pictureMy Functions: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!







#2 Red-Steel

Red-Steel

    Seeker

  • Active Members
  • 33 posts

Posted 07 January 2007 - 02:02 AM

Cool! I was looking for a dec to bin, bin to dec function! Thank you.

#3 argumentum

argumentum

    Wayfarer

  • Active Members
  • Pip
  • 73 posts

Posted 26 December 2008 - 09:30 PM

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, 26 December 2008 - 09:35 PM.


#4 Malkey

Malkey

  • Active Members
  • PipPipPipPipPipPip
  • 1,510 posts

Posted 27 December 2008 - 01:14 PM

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.
AutoIt         
#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


#5 jennico

jennico

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 862 posts

Posted 27 December 2008 - 01:47 PM

well, i made this and other things far more optimized and advanced in my _Primes.au3 UDF. check it out.

cheers j.

Edited by jennico, 27 December 2008 - 01:48 PM.

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.Posted ImageDon't forget this IP: 213.251.145.96

#6 Jango

Jango

    Polymath

  • Active Members
  • PipPipPipPip
  • 202 posts

Posted 29 December 2008 - 08:43 AM

well, i made this and other things far more optimized and advanced in my _Primes.au3 UDF. check it out.

cheers j.


Nice jenico, really nice... thank you for this code

#7 Xwolf

Xwolf

    Seeker

  • Active Members
  • 40 posts

Posted 23 April 2009 - 02:01 PM

Nice code ^_^

#8 nullschritt

nullschritt

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 411 posts

Posted 19 August 2012 - 02:27 AM

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, 19 August 2012 - 02:35 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users