Sign in to follow this  
Followers 0
Alek

UDF _Convert

2 posts in this topic

hello, i made this very small and simple function a while ago and found that it was usfull for more then what i used it for.

i used it to convert diffrent types of numbers into % but i noticed that it could easly be used to convert other things

like celsius into fahrenheit or feet into cm or meters.

you only need to know 2 numbers at the same level from both units.

like in fahrenheit too celsius.

you set the input low to 32

and the input high to 212

you set the output low to 0

and the output high to 100

why 32, 212 and 0, 100?

because 32f = 0c and 212f = 100c

i could use input high as 100 and output high as 37.8 and it whould still work. (37.8c is not presisly 100f, 100f = 37.77777778)

of course you could use formulas to do this but i found it much easyer to just use a function when converting lots of numbers.

_Example()

;###########################################################
;Parameters:    $s_I = The number you want to convert
;               $s_I_Low = The low end of the number, Default = 4
;               $s_I_High = The high end of number, Default = 20
;               $s_O_Low = The low end of the output, Default = 0
;               $s_O_High = The High end of the output, Default = 100
;
;Returns:   A number that has bin scaled to fit your needs, like if you want to convert degrees ( as in a circle) into % then
;           if you input 360 the return whould be 100 or if you input 90 the output whould be 25
;
;Auther:    Alek
;###########################################################
Func _Convert($s_I, $s_I_Low = 4, $s_I_High = 20, $s_O_Low = 0, $s_O_High = 100)
    Return ( ($s_O_High - $s_O_Low) / ($s_I_High - $s_I_Low ) ) * ( $s_I - $s_I_Low ) +  $s_O_Low
EndFunc

Func _Example()
    #include <GUIConstants.au3>

    Local $Input
    Local $Input_Low
    Local $Input_High
    Local $Output_Low
    Local $Output_High

    $Form1 = GUICreate("Converter", 190, 165)

    GUICtrlCreateLabel("Input", 5, 5, 60, 20, $SS_SUNKEN)
    GUICtrlCreateLabel("Input Low", 5, 30, 60, 20, $SS_SUNKEN)
    GUICtrlCreateLabel("Input High", 5, 55, 60, 20, $SS_SUNKEN)
    GUICtrlCreateLabel("Output Low", 5, 80, 60, 20, $SS_SUNKEN)
    GUICtrlCreateLabel("Output High", 5, 105, 60, 20, $SS_SUNKEN)
    GUICtrlCreateLabel("Output", 5, 140, 50, 60, $SS_SUNKEN)

    $Input1 = GUICtrlCreateInput("", 70, 5, 110, 21)
    $Input2 = GUICtrlCreateInput("32", 70, 30, 110, 21) ;32 because 32 = 0 celsius
    $Input3 = GUICtrlCreateInput("212", 70, 55, 110, 21) ;212 because 212 = 100 celsius
    $Input4 = GUICtrlCreateInput("0", 70, 80, 110, 21)
    $Input5 = GUICtrlCreateInput("100", 70, 105, 110, 21)
    $Input6 = GUICtrlCreateInput("", 70, 140, 110, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
    GUISetState(@SW_SHOW)

    While 1
    $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
        EndSwitch
        
        If GUICtrlRead($Input1) <> $Input Or GUICtrlRead($Input1) <> $Input_Low Or GUICtrlRead($Input3) <> $Input_High Or GUICtrlRead($Input4) <> $Output_Low Or GUICtrlRead($Input5) <> $Output_High Then
            If GUICtrlRead($Input1) And GUICtrlRead($Input2) And GUICtrlRead($Input3) And GUICtrlRead($Input4) And GUICtrlRead($Input5) Then
                
                $Input          = GUICtrlRead($Input1)
                $Input_Low      = GUICtrlRead($Input2)
                $Input_High     = GUICtrlRead($Input3)
                $Output_Low     = GUICtrlRead($Input4)
                $Output_High    = GUICtrlRead($Input5)
                
                GUICtrlSetData($Input6, _Convert($Input, $Input_Low, $Input_High, $Output_Low, $Output_High) )
            ElseIf GUICtrlRead($Input6) <> "" Then
                GUICtrlSetData($Input6, "")
            EndIf
        EndIf
    WEnd
EndFunc

thank you for reading :)


[font="Impact"]Never fear, I is here.[/font]

Share this post


Link to post
Share on other sites



Cool! I have been looking around for something like this. I am wanting to make a converter for port/span/card in the dex600 and this may help alot! Thanks! :)

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
Sign in to follow this  
Followers 0