Jump to content

_Hex.au3 - override AutoIt limitations on hexadecimal numbers


jennico
 Share

Recommended Posts

hi there,

working on system variables i stumbled across the (unnecessary ?) limitations in using hex numbers and had to make a workaround. the algebra took some time so i decided to load this up for everyone.

if the developers happen to find this useful, maybe the two little functions could be added to Math.au3.

cheers j.

script: _Hex.au3

#include-once

$pOpt = Opt("MustDeclareVars", 1)

; #INDEX# ================================================================
; Title .........: Hex
; Description ...: This module contains numeric conversions between hexadecimal and
;                  decimal numbers overriding the AutoIt limits / capabilities
; Author ........: jennico (jennicoattminusonlinedotde)
; =======================================================================

Global Const $HX_REF="0123456789ABCDEF"

; #FUNCTION# ==============================================================
; Function Name..: _HexToDec ( "expression" )
; Description ...: Returns decimal expression of a hexadecimal string.
; Parameters ....: expression   - String representation of a hexadecimal expression to be converted to decimal.
; Return values .: Success      - Returns decimal expression of a hexadecimal string.
;                  Failure      - Returns "" (blank string) and sets @error to 1 if string is not hexadecimal type.
; Author ........: jennico (jennicoattminusonlinedotde)
; Remarks .......: working input format: "FFFF" or "0xFFFF" (string format), do NOT pass 0xFFFF without quotation marks (number format).
;                  current AutoIt Dec() limitation: 0x7FFFFFFF (2147483647).
; Related .......: Hex(), Dec(), _DecToHex()
; =======================================================================
Func _HexToDec($hx_hex)
    If StringLeft($hx_hex, 2) = "0x" Then $hx_hex = StringMid($hx_hex, 3)
    If StringIsXDigit($hx_hex) = 0 Then
        SetError(1)
        Return ""
    EndIf
    Local $ret="", $hx_count=0, $hx_array = StringSplit($hx_hex, ""), $Ii, $hx_tmp
    For $Ii = $hx_array[0] To 1 Step -1
        $hx_tmp = StringInStr($HX_REF, $hx_array[$Ii]) - 1
        $ret += $hx_tmp * 16 ^ $hx_count
        $hx_count += 1
    Next
    Return $ret
EndFunc  ;==>_HexToDec()

; #FUNCTION# ==============================================================
; Function Name..: _DecToHex ( expression [, length] )
; Description ...: Returns a string representation of an integer converted to hexadecimal.
; Parameters ....: expression   - The integer to be converted to hexadecimal.
;                  length       - [optional] Number of characters to be returned (no limit).
;                                 If no length specified, leading zeros will be stripped from result.
; Return values .: Success      - Returns a string of length characters representing a hexadecimal expression, zero-padded if necessary.
;                  Failure      - Returns "" (blank string) and sets @error to 1 if expression is not an integer.
; Author ........: jennico (jennicoattminusonlinedotde)
; Remarks .......: Output format "FFFF".
;                  The function will also set @error to 1 if requested length is not sufficient - the returned string will be left truncated.
;                  Be free to modify the function to be working with binary type input - I did not try it though.
;                  current AutoIt Hex() limitation: 0xFFFFFFFF (4294967295).
; Related .......: Hex(), Dec(), _HexToDec()
; =======================================================================
Func _DecToHex($hx_dec, $hx_length = 21)
    If IsInt($hx_dec) = 0 Then
        SetError(1)
        Return ""
    EndIf
    Local $ret = "", $Ii, $hx_tmp, $hx_max
    If $hx_dec < 4294967296 Then 
        If $hx_length < 9 Then Return Hex($hx_dec, $hx_length)
        If $hx_length = 21 Then
            $ret = Hex($hx_dec)
            While StringLeft($ret, 1) = "0"
                $ret = StringMid($ret, 2)
            WEnd
            Return $ret
        EndIf
    EndIf
    For $Ii = $hx_length - 1 To 0 Step -1
        $hx_max = 16 ^ $Ii - 1
        If $ret = "" And $hx_length = 21 And $hx_max > $hx_dec Then ContinueLoop
        $hx_tmp = Int($hx_dec/($hx_max+1))
        If $ret = "" And $hx_length = 21 And $Ii > 0 And $hx_tmp = 0 Then ContinueLoop
        $ret &= StringMid($HX_REF, $hx_tmp+1, 1)
        $hx_dec -= $hx_tmp * ($hx_max + 1)
    Next
    $ret=String($ret)
    If $hx_length < 21 And StringLen($ret) < $hx_length Then SetError(1)
    Return $ret
EndFunc  ;==>_DecToHex()

Opt("MustDeclareVars", $pOpt)
Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

welcome, man. since it's not spectacular, i did not expect any response.

does it work ?

cheers

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

this one is made to put it in the include directory and to be included into your script. don't loose it, it is kinda complicated calculation. you'll spend hours making it yourself.

j.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

  • Moderators

Think you might need to check your script... it seems to be inverted on the output.

For 3933904 yours returned: 3C06D0, When it should have been: D0063C .

If you're really looking to get rid of the limitations with some speed: http://www.autoitscript.com/forum/index.ph...st&p=532633

Edit:

Although I might have missed the point altogether :) .. if so I apologize.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

hello smoke,

Think you might need to check your script... it seems to be inverted on the output.

For 3933904 yours returned: 3C06D0, When it should have been: D0063C .

If you're really looking to get rid of the limitations with some speed: http://www.autoitscript.com/forum/index.ph...st&p=532633

Edit:

Although I might have missed the point altogether .. if so I apologize.

the conversion from integer 3933904 to hexadecimal returns 3C06D0, at least on my calculator. not D0063C. i am not sure what you refer to.

furthermore, for values <4294967296 (this is the AutoIt limit) it uses the normal AutoIt Hex() routine. thus my result MUST be correct and cannot be faster.

If $hx_dec < 4294967296 And $hx_length < 9 Then Return Hex($hx_dec, $hx_length)

(this line though needs some more error handling if it should be used in a legal include)

my script is really fast, it converts about 3000 inputs within 1 second.

don't know, seems you have really missed the point !?!

anyway thanks for your reply. i would like to eliminate all errors that will be reported.

cheers j.

edit: maybe you talk about RGB conversion ?!?

edit: or maybe ascii to hex ?!?

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

  • Moderators

That was my mistake on the conversion, I'll chalk it up to too many hours on the PC.

MsgBox(0, 0, Hex(3933904, 6))

I also think I "kind of" understand now what you were setting out to do.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

well, as i looked deeper into the quoted line, i noticed that there is a logical error in it. will fix it.

j.

Edit: updated, precisized quoted line in _DecToHex()

It's a funny thing with these UDFs. the function only needs like 3 or 5 lines but the error handling blows it up.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

oh, just recently....... :)

well, it's nearly the same. you know, i twisted my brain off for these conversions, why didn't i find this before ? probably, because my principal problem was the limitation, so i worked it out from the other side. i'll check out your algebra now. you made it different in _dectohex().

j.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

@petrex

your converter seems to have an error. i cannot get a conversion from 3933904 integer to hex (above example). returning blank string. you might have to work on that or just use my solution !

btw. the reason why i did not integrate binaries is that Binary() always returns hex, not bin. so i thought i did not understand bin. but now it seems to be AutoIt bug. ?

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

Looks good but I would suggest 1 minor change.

Change

Opt("MustDeclareVars", 1)

to

$pOpt = Opt("MustDeclareVars", 1)

and then change

Opt("MustDeclareVars", 0)

to

Opt("MustDeclareVars", $pOpt)

That way if the user already has Opt("MustDeclareVars") set to 1 for some other reason, you are not arbitrarily changing it on them with Opt("MustDeclareVars", 0).

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

@ GEOSoft

ok, fixed. i never use that option, i only did it to make the script fit the standard conventions.

thx j.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

maybe someone feels free to add binary conversions and others ?

like duodecimal (12-based) ?

happy brain twisting ! :)

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

@ GEOSoft

ok, fixed. i never use that option, i only did it to make the script fit the standard conventions.

thx j.

No problem. I just didn't want to see you set the Opt() value to 0 when they may have needed it at 1 for another reason. Doing it the way I showed will get the current setting for the Opt() and then restore it at the end.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

i understand you, but as far as i know the #includes are always executed first no matter where you put them in your file. you have no chance to set the options before. so all the other udfs and includes that do not care about previous settings should not matter either.

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

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