Sign in to follow this  
Followers 0
donotknownothing

Generating a hex numbers

10 posts in this topic

#1 ·  Posted (edited)

Hi to all,

this is my first project in autoit3 and I am really a newbie. I tried to make small gui utility, but.... after 7 days of reading forum and two pdfs, I only made next code which do not work.

Final goal is to enter some hex value in inputbox1 as starting value, other hex value in inputbox2 as ending value and later (need to add one more inputbox to enter step) to generate (print in file) all values from start to end values. I am far away from it...

Now I just trying to understand some things,so, for start even and sum of two hex values is good enough.

Can You help me with this:

- how to restrict inputbox to accept only hex chars (0-9, and A,B,C,D,E,F) ? I tried to make function for it....but not work.

The best would be to not allow entering non hex values, if it is not possible, then just erasing inputbox with would be fine.

- why MsgBox shows wrong hex values (for example if I enter 1111 and 1111 MsgBox will show 914 instead of 8AE) ?

 

Here is "code"  :idiot: :

#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
Global $x

#Region ### START Koda GUI section ### Form=C:\Users\Administrator\Desktop\Form1.kxf
$Form1 = GUICreate("Form1", 170, 177, 192, 114)
$Input1 = GUICtrlCreateInput("", 48, 40, 97, 21)
GUICtrlSetLimit($Input1,4) ; limits InputBox2 to 4 chars
$Input2 = GUICtrlCreateInput("", 48, 80, 97, 21)
GUICtrlSetLimit($Input2,4) ; limits InputBox2 to 4 chars
$Label1 = GUICtrlCreateLabel("START", 8, 40, 40, 17)
$Label2 = GUICtrlCreateLabel("END", 8, 80, 27, 17)
$Button1 = GUICtrlCreateButton("Generate", 32, 128, 105, 25)
$Label3= GUICtrlCreateLabel("", 8, 105, 150, 25)
GUISetState(@SW_SHOW) ; show form
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
         Case $Button1 ; on Button1 click do next:
            ;GUICtrlSetData ($label3, "New text for label")
            if Inputcheck($Input1) = 0 Then
               GUICtrlSetData($Input1,"")
            EndIf
            MsgBox(0,"NOTICE","You typed: " & (Hex(GUICtrlRead($Input1))+Hex(GUICtrlRead($Input2))))
    EndSwitch
WEnd

Func Inputcheck($x) ; it should check is allowed HEX char entered in InputBox
   Local $y
   $y=StringIsXDigit($x)
   if $y <> 1 Then
      Return 0
   EndIf

EndFunc
Edited by donotknownothing

Share this post


Link to post
Share on other sites



Why do you need it restricted to hex numbers? Hex is just base 16 versus base 10 number scheme. There's nothing magical about hex values.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi,

because my interest (job) is microcontrollers...when send/receive commands, communicate etc.

Almost every script which I ever write will be dedicated to manipulation with hex bytes.

Edited by donotknownothing

Share this post


Link to post
Share on other sites

Yes, my point was that hex numbers aren't anything special. 0-F in hex is nothing more than a different way to display the numbers 0-15 in decimal. You can convert them to hex, but they don't need to be in hex unless the program you're working with doesn't understand base 10 numbering. 

If you really really need them to be entered in hex, there are a lot of examples posted of how to restrict the input on a control.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

My apology, but I can not find examples which describes how to restrict the inputbox on 0 to F. 

Also, I still do not understand why sum of string to hex conversion for values 1111 gives 914 instead of 8AE ?

Edited by donotknownothing

Share this post


Link to post
Share on other sites

I don't know what you mean by "1111 gives 914 instead of 8AE". 0x1111 hex is 4369 decimal, 914 decimal is 0x0392 in hex. Your results make no sense given your posted numbers.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Did You even tried to start posted code and enter 1111 in both input box? Then You Will see what I meant.But never mind,I Just wanted to try with Autoit. I'm back to python for console scripts and Purebasic for small gui utilities. Thanx!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

You mistake is that you read the numbers from the input but these numbers are read as a strings. You have to convert it to integer before you can calculate it with Hex().

Change those lines to add the 2 numbers from the input fields:
 

...
         Case $Button1 ; on Button1 click do next:
            ;GUICtrlSetData ($label3, "New text for label")
            MsgBox(0,"NOTICE","You typed: " & Hex(Int(GUICtrlRead($Input1)) + Int(GUICtrlRead($Input2)), 8))
    EndSwitch
...

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

....

- why MsgBox shows wrong hex values (for example if I enter 1111 and 1111 MsgBox will show 914 instead of 8AE) ?

....

You say hex values are entered into the input boxes.  But decimal 1111 + decimal 1111 = 0x8AE

You will find 0x1111 + 0x1111 = 0x2222

I modified your example.

#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>

Global $x
; - why MsgBox shows wrong hex values (for example if I enter 1111 and 1111 MsgBox will show 914 instead of 8AE) ?
#region ### START Koda GUI section ### Form=C:\Users\Administrator\Desktop\Form1.kxf
$Form1 = GUICreate("Form1", 170, 177, 192, 114)
$Input1 = GUICtrlCreateInput("0x1111", 48, 40, 97, 21)
GUICtrlSetLimit($Input1, 6) ; limits InputBox2 to 4 chars
$Input2 = GUICtrlCreateInput("0x1111", 48, 80, 97, 21)
GUICtrlSetLimit($Input2, 6) ; limits InputBox2 to 4 chars
$Label1 = GUICtrlCreateLabel("START", 8, 40, 40, 17)
$Label2 = GUICtrlCreateLabel("END", 8, 80, 27, 17)
$Button1 = GUICtrlCreateButton("Generate", 32, 128, 105, 25)
$Label3 = GUICtrlCreateLabel("", 8, 105, 150, 25)
GUISetState(@SW_SHOW) ; show form
#endregion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If Inputcheck(GUICtrlRead($Input1)) = 0 Then GUICtrlSetData($Input1, "0")
            If Inputcheck(GUICtrlRead($Input2)) = 0 Then GUICtrlSetData($Input2, "0")
            MsgBox(0, "NOTICE", "Start + End =  " & GUICtrlRead($Input1) & " + " & (GUICtrlRead($Input2)) & " = " & _
                    "0x" & StringRegExpReplace(Hex(Number(GUICtrlRead($Input1)) + Number(GUICtrlRead($Input2))), "^0*", ""))
    EndSwitch
WEnd

Func Inputcheck($x) ; Checks if HEX characters starting with "0x", or decimal characters are entered in InputBoxes
    Local $y
    $y = StringIsXDigit(StringRegExpReplace($x, "^0x", ""))
    If (StringLeft($x, 2) == "0x" And StringIsXDigit(StringTrimLeft($x, 2)) <> 1) Or _   ; hexadecimal number
            (StringLeft($x, 2) <> "0x" And StringIsDigit($x) <> 1) Then                  ; Decimal Number
        Return 0
    Else
        Return 1
    EndIf

EndFunc   ;==>Inputcheck

Share this post


Link to post
Share on other sites

The question I see is how to limit to just hex chars:

#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
Global $x

#Region ### START Koda GUI section ### Form=C:\Users\Administrator\Desktop\Form1.kxf
$Form1 = GUICreate("Form1", 170, 177, 192, 114)
$Input1 = GUICtrlCreateInput("", 48, 40, 97, 21)
GUICtrlSetLimit($Input1,4) ; limits InputBox2 to 4 chars
$Input2 = GUICtrlCreateInput("", 48, 80, 97, 21)
GUICtrlSetLimit($Input2,4) ; limits InputBox2 to 4 chars
$Label1 = GUICtrlCreateLabel("START", 8, 40, 40, 17)
$Label2 = GUICtrlCreateLabel("END", 8, 80, 27, 17)
$Button1 = GUICtrlCreateButton("Generate", 32, 128, 105, 25)
$Label3= GUICtrlCreateLabel("", 8, 105, 150, 25)
GUISetState(@SW_SHOW) ; show form
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
         Case $Button1 ; on Button1 click do next:
            ;GUICtrlSetData ($label3, "New text for label")
            MsgBox(0,"NOTICE","You typed: " & GUICtrlRead($Input1) & ":" & GUICtrlRead($Input2))
    EndSwitch
WEnd

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg
    Local $hWndFrom, $iIDFrom, $iCode
    Local $hInput1 = GUICtrlGetHandle($Input1)
    Local $hInput2 = GUICtrlGetHandle($Input2)
    Local $sText = ""
    $hWndFrom = $lParam
    $iIDFrom = _WinAPI_LoWord($wParam)
    $iCode = _WinAPI_HiWord($wParam)
    Switch $hWndFrom
        Case $hInput1, $hInput2
            Switch $iCode
                Case $EN_CHANGE
                    $sText = _GUICtrlEdit_GetText($hWndFrom)
                    If Not StringRegExp($sText, "(?i)^[0-9a-f]+\z") Then
                        _GUICtrlEdit_SetText($hWndFrom, StringTrimRight($sText, 1))
                        _GUICtrlEdit_SetSel($hWndFrom, StringLen($sText), StringLen($sText))
                        _GUICtrlEdit_ShowBalloonTip($hWndFrom, "Error", "You can only use characters 0-9 or A-F", $TTI_ERROR)
                    EndIf
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Am I missing something else?


[center]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.[/center]

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