CrypticKiwi

BigNum Decimal To Binary

7 posts in this topic

#1 ·  Posted (edited)

Hello Autoit Community,

I have been working on several projects using Autoit  and during that period ive encountered many problems in which Autoit members didn’t hesitate help me each time so as new as I am at scripting and Autoit, this is an ATTEMPT to start paying back to the community.

So, While working on my current project ive encountered a problem in which I needed to convert a 50 - 100 digit number from decimal to binary, my immediate response  was to search the forums for binary to decimal converter or a base to base converter and then found out that the maximum number of digits for conversion is as I remember ~15 digits due to an AutoIt  Limitation, way below my 100 digit requirement, so I used my mind a little bit to solve this problem and attempted to combine BigNum  Author: Eukalyptus   with DectoBase Author: Czardas  and it yielded success.

Original Function _DecToBase

Func _DecToBase($iDecimal, $iBase)
    If Not (IsInt($iDecimal) And IsInt($iBase) And $iBase >= 2 And $iBase <= 16) Then Return SetError(1, 0, "")
    Local $sNewNumber, $iDigit, $iPower = 1, $bNegative = False
    If $iDecimal < 0 Then
        $bNegative = True
        $iDecimal = Abs($iDecimal)
    EndIf
    While $iBase^$iPower <= $iDecimal
        $iPower += 1
    WEnd
    For $i = $iPower -1 To 0 Step -1
        $iDigit = Floor($iDecimal/($iBase^$i))
        $sNewNumber &= StringRight(Hex($iDigit), 1)
        $iDecimal -= $iDigit*($iBase^($i))
    Next
    If $bNegative Then $sNewNumber = "-" & $sNewNumber
    Return $sNewNumber
EndFunc ;==> _DecToBase

New Combined Function

_BigNumDecToBase

Func _BigNumDecToBase($iDecimal, $iBase)
    Local $sNewNumber, $iDigit, $iPower = 1
    Local $Sub
     $X = _BigNum_Compare(_BigNum_Pow($iBase,$iPower),$iDecimal)
    While $X = -1 OR $X = 0
        $X = _BigNum_Compare(_BigNum_Pow($iBase,$iPower),$iDecimal)
        $iPower += 1
    WEnd
    For $i = $iPower -1 To 0 Step -1
        $iDigit = Floor(_BigNum_Div($iDecimal,(_BigNum_Pow($iBase,$i))))
        $sNewNumber &= StringRight(Hex($iDigit), 1)
        $Sub = _BigNum_Mul($iDigit,_BigNum_Pow($iBase,$i))
        $iDecimal = _BigNum_Sub($iDecimal,$Sub)
    Next
    Return $sNewNumber
EndFunc ;==> _BigNumDecToBase

You just need to input the decimal number ($iDecimal) as a string to the function.

I’m sure this will works on other base to base conversion but I only tried it in converting 100 digits from decimal to binary and it worked! yes a little bit slow but way better than nothing...

Now because im new im not sure if I should have taken permission from the authors before posting this and hope that they forgive me.

Also Hope that this helps Someone…

I guess im used to posting in help and support cuz  I think I should have posted this somewhere else.

 

Edited by CrypticKiwi

Share this post


Link to post
Share on other sites



"A little bit slow" is a serious euphemism!

Guys, did you all forget how you learned handwritten division (yes, paper and pencil) or is that just a trick to make people spend money in the coffee machine while the CPU is melting down?

#include "..\include\bignum.au3"

; this is RSA-170, a 170-digit (563-bit) RSA composite (not yet factored)
Local $n = "26062623684139844921529879266674432197085925380486406416164785191859999628542069361450283931914514618683512198164805919882053057222974116478065095809832377336510711545759"
Local $nb = _BigNum_ToBase($n, 13)
ConsoleWrite($nb & @LF)

; this is RSA-2018, a 617-digit (2048-bit) RSA composite (not yet factored)
$n = "25195908475657893494027183240048398571429282126204032027777137836043662020707595556264018525880784406918290641249515082189298559149176184502808489120072844992687392807287776735971418347270261896375014971824691165077613379859095700097330459748808428401797429100642458691817195118746121515172654632282216869987549182422433637259085141865462043576798423387184774447920739934236584823824281198163815010674810451660377306056201619676256133844143603833904414952634432190114657544454178424020924616515723350778707749817125772467962926386356373289912154831438167899885040445364023527381951378636564391212010397122822120720357"
$nb = _BigNum_ToBase($n, 93)
ConsoleWrite($nb & @LF)

Func _BigNum_ToBase($sDecimal, $iBase)
    Local $vDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!""#$%&'()*+,-./:;<=>?@[\]^_`{|}~"      ; 96 ASCII symbols, use Unicode for more
    If $iBase > StringLen($vDigits) Then SetError(1,0,0)
    $sDecimal = StringRegExpReplace($sDecimal, "(\D*0*)(\d+)(.*)", "$2")
    $vDigits = StringSplit($vDigits, '', 2)
    Local $sResult, $sRest
    Do
        $sRest = _BigNum_Mod($sDecimal, $iBase)
        $sResult = $vDigits[$sRest] & $sResult
        $sDecimal = _BigNum_Div(_BigNum_Sub($sDecimal, $sRest), $iBase)
    Until $sDecimal = '0'
    Return $sResult
EndFunc ;==> _BigNum_ToBase

 

1 person likes this

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Forgot to mention its in a loop and ill be doing that 100 digit conversion a lot so thanks to you Jchd ill be saving myself lots of time. 

Share this post


Link to post
Share on other sites

May I ask why you're using such a slow camel as AutoIt to do number crunching? What is your use of that many 100-digit numbers?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Anything I post on the forum can be used without restrictions, unless explicitly stated for non-commercial use, although it is unlikely I will ever place any such restrictions on any of the code I post online. I'm happy to see you managed to combine these functions. It's quite an old function of mine you were using. Also: thanks to jchd for producing yet another masterly rendition of this numeric tocatta. :thumbsup:

Edited by czardas

Share this post


Link to post
Share on other sites

May I ask why you're using such a slow camel as AutoIt to do number crunching? What is your use of that many 100-digit numbers?

Well,  i work on different projects but currently working on a theoretical algorithm for data compression.  I know Autoit is not suitable for this kind of projects but I have my reasons.

 First, im an expert at finding out of the box solutions to specific problems such as Data compression and protection yet,  new to programming and not efficient at all and Autoit is the easiest most flexible language ive encountered yet.

Also this is still in theory and the theories fails on a daily basis so  Im brute forcing the solution by generating about 4-5 new theories per week so I need Autoit’s ease and flexibility to match the speed of theory creation. The Current theory is the most promising thus far and have big chance of working out and If the theory works then I would have the patience and motive to move on to another language. (Giving credit to Autoit ofcourse)

 

Anything I post on the forum can be used without restrictions, unless explicitly stated for non-commercial use, although it is unlikely I will ever place any such restrictions on any of the code I post online. I'm happy to see you managed to combine these functions. It's quite an old function of mine you were using. Also: thanks to jchd for producing yet another masterly rendition of this numeric tocatta. :thumbsup:

Second How could i resist such an awesome community?! :)

 

 

1 person likes this

Share this post


Link to post
Share on other sites

I was meaning that you don't have to use exponentiation (several times) to reach the result.

Incidentally this thread made me [re]aware of a serious issue with AutoIt integral artithmetic which I'm currently investigating.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
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