Jump to content

Sum Hex value of Array


Recommended Posts

Hi i have this script:

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $aArray[8] = ["0x01", "0x02", "0x04", "0x08", "0x10", "0x20", "0x40", "0x80"]
Global $NumberExample = 4
Global $Checkbox_[$NumberExample + 1]
Global $Label_[$NumberExample + 1]
$hGUI = GUICreate("Form1", 347, 270, 192, 124)
$Button = GUICtrlCreateButton("Check", 256, 232, 75, 25)

For $i = 0 To $NumberExample - 1
$Checkbox_[$i] = GUICtrlCreateCheckbox("", 8, 8 + (25 * $i), 15, 15)
$Label_[$i] = GUICtrlCreateLabel("Value " & $i, 25, 9 + (25 * $i), 50, 20)
Next
GUISetState(@SW_SHOW)

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Button
Local $Value = "", $FinalHex = 0
For $i = 0 To $NumberExample - 1
If GUICtrlRead($Checkbox_[$i]) = $GUI_CHECKED Then
     $Value &= $aArray[StringRight(GUICtrlRead($Label_[$i]), 1)] & "|"
EndIf
Next
$aValue = StringSplit($Value, "|")
If $aValue[0] = 2 Then ; One elemen selected Then
Global $FinalHex = $aValue[1]
EndIf
MsgBox(0,0,$FinalHex)
EndSwitch
WEnd

If i select one checkbox i have the correct value, but the problem is i don't know how to sum two or more value of two or more checkbox, exmaple:

I select the first checkbox = Value 0 = 0x01 ( in the script i have do it, don't know the method is correct

I select all checkbox = Value 0 ( 0x01 = 1 ) + Value 1 (0x02 = 2 ) + Value 2 (0x04 = 4 ) + Value 3 (0x08 = 8 )

1+2+4+8 = 15 --> Converted = 0x0f ( i need the 0x0f )

How to do that? Thanks

Edited by MyEarth
Link to comment
Share on other sites

  • Moderators

MyEarth,

I suggest you do this:

; Declare the array like this:
Global $aArray[8] = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80]

; And then change the $Button Case to read:
Case $Button
    Local $Value = "", $FinalHex = 0
    For $i = 0 To $NumberExample - 1
        If GUICtrlRead($Checkbox_[$i]) = $GUI_CHECKED Then
            $FinalHex += $aArray[$i]
        EndIf
    Next
    MsgBox(0, 0, "0x" & Hex($FinalHex, 2))

That gives me the correct result. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

×
×
  • Create New...