Jump to content

EyeSys - Custom number system written in AutoIT!


 Share

Recommended Posts

I was working on a way to reduce 6 byte hex (0xFFFFFF) to a 4 byte string, I ended up coming up with this 64^4 based system

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7
#include-once
;~ Custom Number system by DatMCEyeBall AKA Eye_ZiS

; #INDEX# =======================================================================================================================
; Title .........: EyeSys
; AutoIt Version : v3.3.1.1 or higher
; Language ......: English
; Description ...: Used to decrease 6 byte hex(0xFFFFFF = 16777215) to a 4 byte string using a base of 64. (64^4 = 16777216)
; Note ..........:
; Author(s) .....: DatMCEyeBall AKA Eye_ZiS
; Remarks .......: - Consider that "." means 0 and the "." will be stripped to reduce output size
;                  - If you have a way to improve the code below than please reply to this thread.
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
; _EyeSysEncode: Encodes an Int and returns the EyeCoDE representation of the number
; _EyeSysDecode: Decodes EyeCoDE and returns the Int() representation of the parsed EyeCoDE
; ===============================================================================================================================

Global Const $aStringArray = StringSplit("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "", 2)

;~ Remove the example if you use this UDF in your own code
;~ ~~~~~~~~~~~~~~~~~~~~~~Example~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Local $enc
$enc = _EyeSysEncode(500) ;Returns "5o"
ConsoleWrite("Encoded 500 = " & $enc & @CRLF)
ConsoleWrite("Decoded '" & $enc & "' = " & _EyeSysDecode($enc) & @CRLF) ;Returns 500
;~ ~~~~~~~~~~~~~~~~~~~~End Example~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


; #FUNCTION# ====================================================================================================================
; Name ..........: _EyeSysDecode
; Description ...:
; Syntax ........: _EyeSysDecode($hInEyeCoDE)
; Parameters ....: $hInEyeCoDE          - The EyeCoDE you want to convert to an Int.
; Return values .: Returns the Int() representation of the parsed EyeCoDE
; Author ........: DatMCEyeBall AKA Eye_ZiS
; Modified ......:
; Remarks .......: NOTE: EyeCoDE not more than 4 chars long, but it can be less.
; Related .......:
; Link ..........:
; Example .......: See lines 8-10
; ===============================================================================================================================
Func _EyeSysDecode($hInEyeCoDE)
    If Not IsString($hInEyeCoDE) Then $hInEyeCoDE = String($hInEyeCoDE)
    If StringLen($hInEyeCoDE) > 4 Then
        Return SetError(1, "", "Invalid EyeCoDE")
    ElseIf StringLen($hInEyeCoDE) < 4 Then
        Do
            $hInEyeCoDE = "." & $hInEyeCoDE
        Until StringLen($hInEyeCoDE) = 4
    EndIf

    Local $aTempVar[4], $ScanString
    $ScanString = StringSplit($hInEyeCoDE, "", 2)

    For $x = 0 To 3
        For $y = 0 To 63
            If $ScanString[$x] == $aStringArray[$y] Then
                $aTempVar[$x] = $y
            EndIf
        Next
    Next

    $aTempVar[2] *= 64
    $aTempVar[1] *= 4096
    $aTempVar[0] *= 262144

    Return Int($aTempVar[0] + $aTempVar[1] + $aTempVar[2] + $aTempVar[3])
EndFunc   ;==>_EyeSysDecode


; #FUNCTION# ====================================================================================================================
; Name ..........: _EyeSysEncode
; Description ...:
; Syntax ........: _EyeSysEncode($iInVar)
; Parameters ....: $iInVar              - An integer value to convert to EyeCoDE.
; Return values .: The EyeCoDE representation of your number
; Author ........: DatMCEyeBall AKA Eye_ZiS
; Modified ......:
; Remarks .......: NOTE: _EyeSysEncode() only supports numbers less than 64^4 (16777216)
; Related .......:
; Link ..........:
; Example .......: See lines 8-10
; ===============================================================================================================================
Func _EyeSysEncode($iInVar)
    Local $4, $3, $2, $1, $szReturn

    $1 = $iInVar
    If Not IsInt($iInVar) Then $1 = Int($iInVar)
    If $iInVar > 16777215 Then SetError(1, "", "Value larger than 16777215")

    If $1 >= 262144 Then
        Do
            $1 -= 262144
            $4 += 1
        Until $1 < 262144
    EndIf

    If $1 >= 4096 Then
        Do
            $1 -= 4096
            $3 += 1
        Until $1 < 4096
    EndIf

    If $1 >= 64 Then
        Do
            $1 -= 64
            $2 += 1
        Until $1 < 64
    EndIf

    $szReturn = $aStringArray[$4] & $aStringArray[$3] & $aStringArray[$2] & $aStringArray[$1]
    If StringLeft($szReturn, 1) == "." Then
        Do
            $szReturn = StringTrimLeft($szReturn, 1)
        Until StringLeft($szReturn, 1) <> "."
    EndIf

    Return $szReturn
    ; I call the returned amount EyeCoDE, incase you're wondering.
EndFunc   ;==>_EyeSysEncode

The above shows you how to use arrays and for loops to create an all new number system.

DISCLAIMER: The above was first written in C#, I rewrote it to demonstrate AutoITs' potential.

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Link to comment
Share on other sites

Basically, it's base 64 with the last two characters moved to the front. It's still a positional numeric base number system.

You don't have to limit yourself to 4 characters, there is a very easy way to change between bases: 

Func _ToBase($iNumber, $iBase, $iPad = 1, $sCharSet = Default)
    Local Static $sDefCharSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
    Local $sRet = "", $iDigit

    If $sCharSet = Default Then $sCharSet = $sDefCharSet

    Do
        $iDigit = Mod($iNumber, $iBase)
        $sRet = StringMid($sCharSet, $iDigit + 1, 1) & $sRet
        $iNumber = Int($iNumber / $iBase)
    Until ($iNumber = 0) And (StringLen($sRet) >= $iPad)

    Return $sRet
EndFunc   ;==>_ToBase

Func _FromBase($sNumber, $iBase, $sCharSet = Default)
    Local Static $sDefCharSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
    Local $iRet = 0, $sChar

    If $sCharSet = Default Then $sCharSet = $sDefCharSet

    Do
        $iRet *= $iBase
        $sChar = StringLeft($sNumber, 1)
        $iRet += StringInStr($sCharSet, $sChar, 1) - 1
        $sNumber = StringTrimLeft($sNumber, 1)
    Until $sNumber = ""

    Return $iRet
EndFunc   ;==>_FromBase

Just change the character set used by those functions and you'll get the same number system as yours. The way it works is exactly how you've done it, but with cleverer maths so that it can be done in a loop with no hard coded constants.

Strings are also better for this, as using StringInStr is a lot easier than searching an array manually.

Nice to see people looking at bases. It's a fascinating topic that I've spent a lot of time researching. In fact I was writing a document that explained numeric bases including negabases and started to look at complex bases... This is what I had so far: link.

Link to comment
Share on other sites

Thats very... Interesting, I might look into using this to create a program that does... well something - I still need to think.

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

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