Sign in to follow this  
Followers 0
crashdemons

Range Conversions

3 posts in this topic

#1 ·  Posted (edited)

Quick functions for converting numbers in a range.

For Instance Convert 12 in a range from -8 to 90 to a proportional equivalent in 0 -to- 100 (or 78 -to- 900)

simple math but easier than retyping it

Returns Float or Int

_ConvertRange(Input Value, First Range Minimum, First Range Max, Second Range Min, Second Range Max)

Returns Float or Int

_ToPercent(Input Value, First Range Minimum, First Range Max)

(same as _ConvertRange(Input Value, First Range Minimum, First Range Max, 0, 100) )

For whatever you need them for.....

I *Believe* these functions work correctly...

they were probably created repetatively in thousands of codes but I after toiling to find them for certain usefulness

I figured I'd put them into AutoIt Functions for fun...

Note: make sure that the minimums are actually below the maximums (not by value..... -12 is smaller than -2 or 1 ... etc.)

Func _ConvertRange($i, $n, $x, $nb, $xb)
    ;converts a number in a range of numbers
    ;proportionally to number in another range
    ;despite whether the numbers are negative or not
    $C = ((($i - $n) / ($x - $n)) * ($xb - $nb)) + $nb
    Return $C
EndFunc
; Note: Sometimes it's necessary to find an InvertRange number, this is simply done:
;_ConvertRange($range_input, $range_min, $range_x,         $range_x, $range_min)
; eg:   (10,   0,100)=90     (1,    1,88)=88


Func _ToPercent($i, $n, $x)
    $P = 100 * (($i - $n) / ($x - $n));
    Return $P
EndFunc
Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Since I dont feel like wasting another thread on these simple but useful funcs,

ill just add it to my current post.

Just alot of IP useful/stupid funcs

some have come along and been rewritten from when I created them in PHP

Modifications and improvements welcome

_IPChr - Converts an IP string to a 4-byte string

_IPStr - Converts a 4-byte string to a IP string

_IPArray - Output Array in a 4-element array (yeah I know, actually 5 unnecessarily)

_IPArrayStr - Array to IP

_IPBlockMax - Finds the maximum IP in an IP-Block

_IsIPinBlock - Tests whether an IP is inside the given IP-Block

_CalcIP - Outputs an IP in decimal form (easier to do range and distance operations)

_DecalcIP - Outputs a decimal-form IP as an actual IP

_IsIPInRange - Tests whether an IP is between two other IP's

_RangeIP - Finds the distance between two IP's (distance measured in decimal)

_MovIP - Finds the IP which is a given distance from the given IP

Func _IPChr($sIP)
    Local $aIP, $cIP=''
    $aIP=StringSplit($sIP,'.')
    For $i=1 To UBound($aIP)-1
        $cIP&=Chr($aIP[$i])
    Next
    $aIP=''
    Return $cIP
EndFunc
Func _IPStr($cIP)
    Local $aIP, $sIP=''
    $aIP=StringSplit($cIP,'')
    If (UBound($aIP)-1)<4 Then Return -1
    For $i=1 To 4
        $sIP&=Asc($aIP[$i])
        if $i<4 then $sIP&='.'
    Next
    $aIP=''
    Return $sIP
EndFunc
Func _IPArray($IP)
    $aIP=StringSplit($IP,'.')
    ;$uIP=Ubound($aIP)-1
    ;For $i=1 To $uIP
    ;   $aIP[$i]=_IPSegmentNormalize($aIP[$i])
    ;Next
    Return $aIP
EndFunc
Func _IPArrayStr($aIP,$start=1)
    Dim $start
    Return $aIP[$start]&'.'&$aIP[$start+1]&'.'&$aIP[$start+2]&'.'&$aIP[$start+3]
EndFunc
Func _CalcIP($IP)
    ;;Calculates and IP as a decimal value
    $ip_array=_IPArray($IP)
    Return ($ip_array[4] + ($ip_array[3]*256) + ($ip_array[2]*65536) + ($ip_array[1]*16777216));

EndFunc
func _DecalcIP($ipv)
    ;;Calculates an IP from a decimal value
    return _MovIP("0.0.0.0", $ipv);
EndFunc
Func _IsIPInRange($IP,$IPa,$IPb)
    ;; Checks whether an IP is between two other IP's
    $IPa=_CalcIP($IPa)
    $IPv=_CalcIP($IP)
    $IPb=_CalcIP($IPb)
    If $IPa<=$IPv And $IPv<=$IPb Then Return True
    Return False
EndFunc



Func _RangeIP($ipa,$ipb)
    ;;Determines the distance between two IP's (by number of IP's between them)
        $ipa=_CalcIP($ipa);
        $ipb=_CalcIP($ipb);
        if $ipa>$ipb Then 
            $ipt=$ipa
            $ipa=$ipb
            $ipb=$ipt
            $ipt='';
        EndIf
        $dist=Abs($ipb - $ipa);
        return $dist;
EndFunc
Func _IPBlockMax($IP_block)
    ;;Returns the maximum IP in the IP block
    ; IP/Block eg:    0.0.0.0/8    _IPBlockMax('0.0.0.0/8') = 0.255.255.255
    $aBL=StringSplit($IP_block,'/')
    $IP=$aBL[1]
    If Ubound($aBL)<3 Then MsgBox("Program Error","IP-Block Error: '"&$IP_block&"' is an invalid IPv4 Block.")
    $block=$aBL[2]
    $aBL=''
    $number_of_hosts=2^(32 - $block)-1
    Return _MovIP($IP, $number_of_hosts)
EndFunc
Func _IsIPinBlock($IP,$IP_block)
    ;; Checks whether an IP is in the IP block specified
    $aBL=StringSplit($IP_block,'/')
    $IPa=_CalcIP($aBL[1])
    $IPv=_CalcIP($IP)
    $IPb=_CalcIP(_IPBlockMax($IP_block))
    $aBL=''
    If $IPa<=$IPv And $IPv<=$IPb Then Return True
    Return False
EndFunc
Func _MovIP($ip, $change)
    ;;Moves and IP forward or backward by a number of IP's
;4294967295 is the maximum change value if you start at 0.0.0.0 (this will change to 255.255.255.255 using decalcip();)
    $ip_array=_IPArray($IP)
    If $change<0 Then
        $ip_array[1]=0
        $ip_array[2]=0
        $ip_array[3]=0
        $ip_array[4]=_CalcIP($ip)
        If $ip_array[4]<Abs($change) Then Return '0.0.0.0'
    EndIf
    
    $ip_array[4]=$ip_array[4] + $change;
    $ipend=StringSplit('.....','.')
    $ipend[4]=16777216;
    $ipend[3]=65536;
    $ipend[2]=256;
    $ipend[1]=0;
    For $i=4 To 1 Step -1
        For $ii=4 To 2 Step -1
            while $ip_array[$i]>($ipend[$ii]-1)
                $ip_array[$i]-=$ipend[$ii]
                $ip_array[$i-($ii-1)]+=1;
            WEnd
        Next
        If $ip_array[1]>($ipend[2]-1) Then Return '255.255.255.255'
    Next
    return _IPArrayStr($ip_array)
EndFuncoÝ÷ ØrÙí±ç­ë~éܶ*'±él{^­æî¶Ø^ÉÊ.×±ËF QÞi׫~éÜ°*.Ô¶Èî²Ö­ªiÉû§rÚ¢éí&j|­g¬j{¦mêÞ½éåÊ
²)í¡©è¶«jw`÷rW·¦j)Ýz±g­)ȨÄ^¶¸½ë-è^i׫Â'm©Ýè Ú]jÖ T´8¦z{"¢{zÛ«Ëaz¯zqÖ­k­©¨v'âyØ^i׫Â'm©Ýè Ö®¶­sdgVæ2ô6÷VçE6WG2b33c¶Âb33c¶ ¶6÷VçG2FR6WG2öbb33c¶âb33c¶æBFR&VÖæFW  ²b33c¶6÷VÆB&R6ÖÆÆW"Fâb33c¶ b33c·#Õ7G&æu7ÆBb33²ââb33²Âb33²b33² b33c·%³ÓÓ b33c·%³ÓÓ bb33c¶fwC²b33c¶FVà b33c·%³ÓÒb33c¶ &WGW&âb33c·  VæD` bb33c¶Òb33c¶FVà b33c·%³ÓÓ b33c·%³ÓÓ &WGW&âb33c·  VæD` b33c·%³ÓÔçBb33c¶òb33c¶ b33c·%³ÓÒb33c¶Òb33c·%³Ò¢b33c¶ &WGW&âb33c· ¤VæDgVæ0¤gVæ2ôtdvWDFÖVç6öç2b33c¶vfFF ·&WG&WfW2FRvGFæBVvBtbBÖ'FRFÖVç6öâVFW  b33c·#Õ7G&æu7ÆBb33²âb33²Âb33²b33² b33c·SÔ627G&ætÖBb33c¶vfFFÃã#S` b33c·S#Ô627G&ætÖBb33c¶vfFFÃã#S` b33c·%³ÓÔ627G&ætÖBb33c¶vfFFÃròb33c·S b33c·%³ÓÔ627G&ætÖBb33c¶vfFFÃòb33c·S  &WGW&âb33c· ¤VæDgVæ0¤gVæ2ôte6WDFÖVç6öç2b33c¶vfFFÂb33c·sÓÂb33c¶Ó ·6WG2FRBÖ'FRFÖVç6öâVFW"W6ærFRvfVâvGFæBVv@ FÒb33c·rÂb33c¶ b33c·SÓ b33c·S#Ó b33c¶óÕô6÷VçE6WG2b33c·rÃ#Sb b33c¶ó#Õô6÷VçE6WG2b33c¶Ã#Sb b33c·SÒb33c¶ó³Ð b33c·sÒb33c¶ó³Ð b33c·S#Òb33c¶ó%³Ð b33c¶Òb33c¶ó%³Ð b33c¶ÆVãÔ6"²b33c·rf×´6"²b33c·Sf×´6"²b33c¶f×´6"²b33c·S" b33c¶vfFFÕõ7G&æu&WÆ6U6V7Föâb33c¶vfFFÃrÃÂb33c¶ÆVâ &WGW&âb33c¶vfFF¤VæDgVæ0
Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites

... Reply Editbox is broken trying to edit my last post... hrmmm anyway I forgot some things:

String Functions (one is required in GIF header editing)

_StringReplaceSection - Replaces a section of a string with a substring using original-string positions alone

_StringInject - Implants a substring inside a string at the given position

Func _StringReplaceSection($string,$start,$end, $substring)
    $first=''
    $last=''
    $l=StringLen($string)
    If ($start-1)>0 Then $first=StringMid($string,1,$start-1)
    If ($end+1)<=$l Then $last=StringMid($string,$end+1)
    ;For $i=1 To $l
    ;   If $i<$start Then $first&=StringMid($string,$i,1)
    ;   If $i>$end Then $last&=StringMid($string,$i,1)
    ;Next
    Return $first&$substring&$last
EndFunc
Func _StringInject($string, $position, $substring)
    If $position<=1 Then Return $substring&$string
    If $position>StringLen($string) Then Return $string&$substring
    $d1=''
    If ($position-1)>0 Then $d1=StringMid($string,1,$position-1)
    $d2=$substring
    $d3=StringMid($string,$position)
    Return $d1&$d2&$d3
EndFunc

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

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