Jump to content

Base Conversion


crzftx
 Share

Recommended Posts

$gui = GUICreate("Base Conversion",500,110)

GUICtrlCreateLabel("Input Number:",10,10,85,20)
$guiInput = GUICtrlCreateInput("",95,10,330,20)
GUICtrlCreateLabel("Base:",435,10,35,20)
$guiInputBase = GUICtrlCreateInput("",470,10,20,20,0x2000)

GUICtrlCreateLabel("Output Number:",10,40,85,20)
$guiOutput = GUICtrlCreateInput("",95,40,330,20,0x2800)
GUICtrlCreateLabel("Base:",435,40,35,20)
$guiOutputBase = GUICtrlCreateInput("",470,40,20,20)

$convertButton = GUICtrlCreateButton("Convert",200,70,100,30)

GUISetState()

$dll = DllOpen("user32.dll")

While 1
    $msg = GUIGetMsg()
    $enterKey = DllCall($dll,"int","GetAsyncKeyState","int","0x0d")
    Select
        Case $msg = -3
            Exit
        Case $msg = $convertButton Or ($enterKey[0] And WinActive($gui))
            ;convert to base-10
            $firstOutput = 0
            $firstInput = String(GUICtrlRead($guiInput))
            $firstInputBase = Number(GUICtrlRead($guiInputBase))
            If ErrorCheck($firstInput,$firstInputBase,Number(GUICtrlRead($guiOutputBase))) Then 
                While $firstInput > ""
                    $firstOutput += BaseChar(StringLeft($firstInput,1)) * $firstInputBase^(Number(StringLen($firstInput)-1))
                    $firstInput = StringTrimLeft($firstInput,1)
                WEnd
                ;convert the base-10 to the other base
                $secondOutput = ""
                $secondInput = $firstOutput
                $outputBase = Number(GUICtrlRead($guiOutputBase))
                $digits = Int(Log($secondInput)/Log($outputBase))
                For $z = $digits to 0 step -1
                    $secondOutput = $secondOutput & BaseChar(String(Int($secondInput / $outputBase^$z)))
                    $secondInput -= Int($secondInput / $outputBase^$z)*$outputBase^$z
                Next
                GUICtrlSetData($guiOutput,$secondOutput)
                GUICtrlSetState($guiInput,256)
            EndIf
    EndSelect
WEnd

Func ErrorCheck($ec_input,$ec_inputBase,$ec_outputBase)
    If StringIsAlNum($ec_input)=0 Or $ec_input = "" Then
        GUICtrlSetState($guiInput,256)
        Return 0
    EndIf
;~  If $ec_inputBase < 2 Or $ec_inputBase > 36 Then
    If $ec_inputBase < 2 Then
        GUICtrlSetState($guiInputBase,256)
        Return 0
    EndIf
    If $ec_outputBase < 2 Or $ec_outputBase > 36 Then
        GUICtrlSetState($guiOutputBase,256)
        Return 0
    EndIf
    For $z = 1 To StringLen($ec_input)
        If BaseChar(StringMid($ec_input,$z,1)) >= $ec_inputBase Then
            GUICtrlSetState($guiInput,256)
            Return 0
        EndIf
    Next
    Return 1
EndFunc


Func BaseChar($char)
    If StringIsAlpha($char) Then
        If Asc($char) > 96 Then
            Return Number(Asc($char) - 87)
        Else
            Return Number(Asc($char) - 55)
        EndIf
        
    Else
        If Number($char) < 10 Then
            Return Number($char)
        Else
            Return Chr($char+55)
        EndIf
    EndIf
EndFunc

Link to comment
Share on other sites

$gui = GUICreate("Base Conversion",500,110)

GUICtrlCreateLabel("Input Number:",10,10,85,20)
$guiInput = GUICtrlCreateInput("",95,10,330,20)
GUICtrlCreateLabel("Base:",435,10,35,20)
$guiInputBase = GUICtrlCreateInput("",470,10,20,20,0x2000)

GUICtrlCreateLabel("Output Number:",10,40,85,20)
$guiOutput = GUICtrlCreateInput("",95,40,330,20,0x2800)
GUICtrlCreateLabel("Base:",435,40,35,20)
$guiOutputBase = GUICtrlCreateInput("",470,40,20,20)

$convertButton = GUICtrlCreateButton("Convert",200,70,100,30)

GUISetState()

$dll = DllOpen("user32.dll")

While 1
    $msg = GUIGetMsg()
    $enterKey = DllCall($dll,"int","GetAsyncKeyState","int","0x0d")
    Select
        Case $msg = -3
            Exit
        Case $msg = $convertButton Or ($enterKey[0] And WinActive($gui))
            ;convert to base-10
            $firstOutput = 0
            $firstInput = String(GUICtrlRead($guiInput))
            $firstInputBase = Number(GUICtrlRead($guiInputBase))
            If ErrorCheck($firstInput,$firstInputBase,Number(GUICtrlRead($guiOutputBase))) Then 
                While $firstInput > ""
                    $firstOutput += BaseChar(StringLeft($firstInput,1)) * $firstInputBase^(Number(StringLen($firstInput)-1))
                    $firstInput = StringTrimLeft($firstInput,1)
                WEnd
                ;convert the base-10 to the other base
                $secondOutput = ""
                $secondInput = $firstOutput
                $outputBase = Number(GUICtrlRead($guiOutputBase))
                $digits = Int(Log($secondInput)/Log($outputBase))
                For $z = $digits to 0 step -1
                    $secondOutput = $secondOutput & BaseChar(String(Int($secondInput / $outputBase^$z)))
                    $secondInput -= Int($secondInput / $outputBase^$z)*$outputBase^$z
                Next
                GUICtrlSetData($guiOutput,$secondOutput)
                GUICtrlSetState($guiInput,256)
            EndIf
    EndSelect
WEnd

Func ErrorCheck($ec_input,$ec_inputBase,$ec_outputBase)
    If StringIsAlNum($ec_input)=0 Or $ec_input = "" Then
        GUICtrlSetState($guiInput,256)
        Return 0
    EndIf
;~  If $ec_inputBase < 2 Or $ec_inputBase > 36 Then
    If $ec_inputBase < 2 Then
        GUICtrlSetState($guiInputBase,256)
        Return 0
    EndIf
    If $ec_outputBase < 2 Or $ec_outputBase > 36 Then
        GUICtrlSetState($guiOutputBase,256)
        Return 0
    EndIf
    For $z = 1 To StringLen($ec_input)
        If BaseChar(StringMid($ec_input,$z,1)) >= $ec_inputBase Then
            GUICtrlSetState($guiInput,256)
            Return 0
        EndIf
    Next
    Return 1
EndFunc


Func BaseChar($char)
    If StringIsAlpha($char) Then
        If Asc($char) > 96 Then
            Return Number(Asc($char) - 87)
        Else
            Return Number(Asc($char) - 55)
        EndIf
        
    Else
        If Number($char) < 10 Then
            Return Number($char)
        Else
            Return Chr($char+55)
        EndIf
    EndIf
EndFunc

That's very much better than my attempt. Very good.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...