Jump to content

Recommended Posts

Posted

How to make a function?

_Num1_To_Num2($num, '0123456789ABCDEFGHIJK', 'abcdefghijklmnopqrstuvwxyz')

I can convert using these functions _DecToNum, _NumToDec. But there is a limit to 9223372036854775807 to decimal numbers. There is an algorithm shift, but I did not understand.

$dec = 250

$timer = TimerInit()
$n = _
        _DecToNum($dec, 'abcdefghijklmnopqrstuvwxyz') & ', R= ' & @extended & @CRLF & _
        _DecToNum($dec, '0123456789ABCDEF') & ', R= ' & @extended & @CRLF & _
        _DecToNum($dec, '0123456789') & ', R= ' & @extended & @CRLF & _
        _DecToNum($dec, '01') & ', R= ' & @extended
MsgBox(0, $dec & ' за ' & Round(TimerDiff($timer), 2) & ' мсек', $n)

$timer = TimerInit()
$n = _
        _NumToDec('ytwp', 'abcdefghijklmnopqrstuvwxyz') & ', R= ' & @extended & @CRLF & _
        _NumToDec('6A437', '0123456789ABCDEF') & ', R= ' & @extended & @CRLF & _
        _NumToDec('435255', '0123456789') & ', R= ' & @extended & @CRLF & _
        _NumToDec('1101010010000110111', '01') & ', R= ' & @extended
MsgBox(0, ' за ' & Round(TimerDiff($timer), 2) & ' мсек', $n)

; ============================================================================================
; Function Name ...: _NumToDec
; Description ........: Converts a number into a decimal
; Syntax.......: _NumToDec($num, $sSymbol)
; Parameters:
;       $num - number
;       $sSymbol - A set of symbols defining the sequence
;       $casesense - (0,1,2) corresponds to flag StringInStr
; Return values: Success - Returns a decimal number, @extended - the number of characters in the discharge
;                   Failure - Returns $num, and sets @error:
;                  |1 - $sSymbol contains less than 2 characters
;                  |2 - The symbol is not found in the character set
; Author(s) ..........: AZJIO
; Remarks ..........: For the 16 - Dec
; ============================================================================================
Func _NumToDec($num, $sSymbol, $casesense = 0)
    Local $i, $iPos, $Len, $n, $Out
    $Len = StringLen($sSymbol)
    If $Len < 2 Then Return SetError(1, 0, $num)
    
    $n=StringSplit($num, '')
    For $i = 1 To $n[0]
        $iPos = StringInStr($sSymbol, $n[$i], $casesense)
        If Not $iPos Then Return SetError(2, 0, $num)
        $Out += ($iPos-1)*$Len^($n[0]-$i)
    Next
    Return SetError(0, $Len, $Out)
EndFunc

; ============================================================================================
; Function Name ...: _DecToNum
; Description ........: Converts a number from the decimal system to the specified
; Syntax.......: _DecToNum ( $iDec, $Symbol )
; Parameters:
;       $iDec - decimal number
;       $Symbol - A set of symbols defining the sequence
; Return values: Success - Returns the new number, @extended - the number of characters in the discharge
;                   Failure - Returns $iDec, @error = 1
; Author(s) ..........: AZJIO
; Remarks ..........: For the 16, 8 - Hex, StringFormat
; ============================================================================================
Func _DecToNum($iDec, $Symbol)
    Local $Out, $ost
    $Symbol = StringSplit($Symbol, '')
    If @error Or $Symbol[0] < 2 Then Return SetError(1, 0, $iDec)
    Do
        $ost = Mod($iDec, $Symbol[0])
        $iDec = ($iDec - $ost) / $Symbol[0]
        $Out = $Symbol[$ost + 1] & $Out
        ; $k+=1
    Until Not $iDec
    Return SetError(0, $Symbol[0], $Out)
EndFunc
Posted

I am not really sure what the question is, but here is one possible answer.

Local $num = "1DZ2ZK4"
Local $timer = TimerInit()
Local $n = _Num1_To_Num2($num, '0123456789ABCDEJKLMYZ', 'abcdefghijklmnopqrstuvwxyz')
MsgBox(0, Round(TimerDiff($timer), 2), $num & " = " & $n)


Func _Num1_To_Num2($num, $sSymbolFrom, $sSymbolTo)
    Local $temp = _NumToDec($num, $sSymbolFrom)
    Return _DecToNum($temp, $sSymbolTo)
EndFunc   ;==>_Num1_To_Num2

; ============================================================================================
; Function Name ...: _NumToDec
; Description ........: Converts a number into a decimal
; Syntax.......: _NumToDec($num, $sSymbol)
; Parameters:
;      $num - number
;      $sSymbol - A set of symbols defining the sequence
;      $casesense - (0,1,2) corresponds to flag StringInStr
; Return values: Success - Returns a decimal number, @extended - the number of characters in the discharge
;                  Failure - Returns $num, and sets @error:
;                 |1 - $sSymbol contains less than 2 characters
;                 |2 - The symbol is not found in the character set
; Author(s) ..........: AZJIO
; Remarks ..........: For the 16 - Dec
; ============================================================================================
Func _NumToDec($num, $sSymbol, $casesense = 0)
    Local $i, $iPos, $Len, $n, $Out
    $Len = StringLen($sSymbol)
    If $Len < 2 Then Return SetError(1, 0, $num)

    $n = StringSplit($num, '')
    For $i = 1 To $n[0]
        $iPos = StringInStr($sSymbol, $n[$i], $casesense)
        If Not $iPos Then Return SetError(2, 0, $num)
        $Out += ($iPos - 1) * $Len ^ ($n[0] - $i)
    Next
    Return SetError(0, $Len, $Out)
EndFunc   ;==>_NumToDec

; ============================================================================================
; Function Name ...: _DecToNum
; Description ........: Converts a number from the decimal system to the specified
; Syntax.......: _DecToNum ( $iDec, $Symbol )
; Parameters:
;      $iDec - decimal number
;      $Symbol - A set of symbols defining the sequence
; Return values: Success - Returns the new number, @extended - the number of characters in the discharge
;                  Failure - Returns $iDec, @error = 1
; Author(s) ..........: AZJIO
; Remarks ..........: For the 16, 8 - Hex, StringFormat
; ============================================================================================
Func _DecToNum($iDec, $Symbol)
    Local $Out, $ost
    $Symbol = StringSplit($Symbol, '')
    If @error Or $Symbol[0] < 2 Then Return SetError(1, 0, $iDec)
    Do
        $ost = Mod($iDec, $Symbol[0])
        $iDec = ($iDec - $ost) / $Symbol[0]
        $Out = $Symbol[$ost + 1] & $Out
        ; $k+=1
    Until Not $iDec
    Return SetError(0, $Symbol[0], $Out)
EndFunc   ;==>_DecToNum
Posted

Yes, I know, but wanted to use in a pinch. Because works slower at 100 times. And is double conversion.

#include <BigNum.au3>
#include <NumToNum.au3>

$md5 = '5DD66C671119146C30CC27FB9A138733'
$sSymbol = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
$timer = TimerInit()
$n =_DecToNum(_NumToDec($md5, '0123456789ABCDEF'), $sSymbol)
MsgBox(0, Round(TimerDiff($timer), 2) & ' msec', $n)

NumToNum.au3

#include <BigNum.au3>

; ============================================================================================
; Function Name ...: _NumToDec
; Description ........: Converts a number into a decimal
; Syntax.......: _NumToDec($num, $sSymbol)
; Parameters:
;       $num - number
;       $sSymbol - A set of symbols defining the sequence
;       $casesense - (0,1,2) corresponds to flag StringInStr
; Return values: Success - Returns a decimal number, @extended - the number of characters in the discharge
;                   Failure - Returns $num, and sets @error:
;                  |1 - $sSymbol contains less than 2 characters
;                  |2 - The symbol is not found in the character set
; Author(s) ..........: AZJIO
; Remarks ..........: For the 16 - Dec
; ============================================================================================
Func _NumToDec($num, $sSymbol, $casesense = 0)
    Local $i, $iPos, $Len, $n, $Out
    $Len = StringLen($sSymbol)
    If $Len < 2 Then Return SetError(1, 0, $num)
    
    $n = StringSplit($num, '')
    For $i = 1 To $n[0]
        $iPos = StringInStr($sSymbol, $n[$i], $casesense)
        If Not $iPos Then Return SetError(2, 0, $num)
        $Out = _BigNum_Add(_BigNum_Mul($iPos - 1, _BigNum_Pow($Len, $n[0] - $i)), $Out)
    Next
    Return SetError(0, $Len, $Out)
EndFunc

; ============================================================================================
; Function Name ...: _DecToNum
; Description ........: Converts a number from the decimal system to the specified
; Syntax.......: _DecToNum ( $iDec, $Symbol )
; Parameters:
;       $iDec - decimal number
;       $Symbol - A set of symbols defining the sequence
; Return values: Success - Returns the new number, @extended - the number of characters in the discharge
;                   Failure - Returns $iDec, @error = 1
; Author(s) ..........: AZJIO
; Remarks ..........: For the 16, 8 - Hex, StringFormat
; ============================================================================================
Func _DecToNum($iDec, $Symbol)
    Local $Out, $ost
    $Symbol = StringSplit($Symbol, '')
    If @error Or $Symbol[0] < 2 Then Return SetError(1, 0, $iDec)
    Do
        $ost = _BigNum_Mod($iDec, $Symbol[0])
        $iDec = _BigNum_Div(_BigNum_Sub($iDec, $ost), $Symbol[0])
        $Out = $Symbol[$ost + 1] & $Out
    Until Not Number($iDec)
    Return SetError(0, $Symbol[0], $Out)
EndFunc
Posted (edited)

Malkey

example of a large number (md5) causes an error.

I was hoping that there is a logical transformation that manipulates numbers less than the number of characters in the set

Edited by AZJIO

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
  • Recently Browsing   0 members

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