TimRude Posted December 22, 2022 Posted December 22, 2022 If I have an 8-digit (32-bit) hex value such as 0x12345678 and I want to split the digits into 2 halves (i.e. 0x1234 and 0x5678), what's the best way to do it? I'm not talking about numerically dividing the value in half. I need to chop the hex digits in half. Here's the method I'm using that works but may not be the most efficient since I'm converting back and forth between numbers and strings: Local $iValFull = 0x12345678 Local $sHexDigits = Hex($iValFull, 8) ; convert number to string of 8 hex digits without the 0x prefix Local $iVal1 = Number("0x" & StringLeft($sHexDigits, 4)) ; convert the first 4 hex digits to new number Local $iVal2 = Number("0x" & StringRight($sHexDigits, 4)) ; convert the last 4 digits to new number ; Display split in both hex and decimal formats to confirm that we end up with numeric values MsgBox(0, "Split Hex", _ "HEX: 0x" & Hex($iValFull) & " --> 0x" & Hex($iVal1, 4) & " and 0x" & Hex($iVal2, 4) & _ @CRLF & @CRLF & _ "DEC: " & $iValFull & " --> " & $iVal1 & " and " & $iVal2) Better ways?
SOLVE-SMART Posted December 22, 2022 Posted December 22, 2022 Hi @TimRude, I tried it with a StringRegExpย variant, butย it's neither more efficientย nor shorter or elegant. I hope some other people here will come up with a different variant - I am curious about it too.Best regardsSven________________Stay innovative! ==> AutoIt related: ๐ GitHub, ๐ Discord Server Spoiler ๐ย Au3Forums ๐ฒ AutoIt (en) Cheat Sheet ๐ AutoIt limits/defaults ๐ Code Katas: [...] (comming soon) ๐ญ Collection of GitHub users with AutoIt projects ๐ย False-Positives ๐ฎย Me on GitHub ๐ฌย Opinion about new forum sub category ๐ย UDF wiki list โย VSCode-AutoItSnippets ๐ย WebDriver FAQs ๐จโ๐ซย WebDriver Tutorial (coming soon)
Solution RTFC Posted December 22, 2022 Solution Posted December 22, 2022 Local $iValFull = 0x12345678 Local $iVal1 = BitShift($iValFull,16) Local $iVal2 = BitAND($iValFull,0xffff) ; Display split in both hex and decimal formats to confirm that we end up with numeric values MsgBox(0, "Split Hex", _ "HEX: 0x" & Hex($iValFull) & " --> 0x" & Hex($iVal1, 4) & " and 0x" & Hex($iVal2, 4) & _ @CRLF & @CRLF & _ "DEC: " & $iValFull & " --> " & $iVal1 & " and " & $iVal2) ย SOLVE-SMART and TimRude 1 1 My Contributions and Wrappers Spoiler BitMaskSudokuSolverย BuildPartitionTable ย CodeCrypter ย CodeScanner ย DigitalDisplay ย Eigen4AutoIt ย FAT Suite ย ย HighMem ย MetaCodeFileLibrary ย OSgrid ย Pool ย RdRand ย SecondDesktop ย SimulatedAnnealing ย Xbase I/O
SOLVE-SMART Posted December 22, 2022 Posted December 22, 2022 Hi @RTFC, wow, that's impressive ๐ . The first new thing that I learned today (AutoIt context). Thank you.Best regardsSven________________Stay innovative! ==> AutoIt related: ๐ GitHub, ๐ Discord Server Spoiler ๐ย Au3Forums ๐ฒ AutoIt (en) Cheat Sheet ๐ AutoIt limits/defaults ๐ Code Katas: [...] (comming soon) ๐ญ Collection of GitHub users with AutoIt projects ๐ย False-Positives ๐ฎย Me on GitHub ๐ฌย Opinion about new forum sub category ๐ย UDF wiki list โย VSCode-AutoItSnippets ๐ย WebDriver FAQs ๐จโ๐ซย WebDriver Tutorial (coming soon)
Nine Posted December 22, 2022 Posted December 22, 2022 Another way : #include <Array.au3> Local $iValFull = 0x12345678 Local $sValue = Hex($iValFull, 8) Local $aValue = StringRegExp($sValue, "(.{4})(.{4})", 1) _ArrayDisplay($aValue) ย SOLVE-SMART 1 โThey did not know it was impossible, so they did itโย โ Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text ย Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker ย Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC ย HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet ย Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector ย Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF ย Multi-Threading Made Easy Interface Object based on Tag ย
TimRude Posted December 22, 2022 Author Posted December 22, 2022 @RTFCย I think we have a winner. Elegant and direct with no string conversions. @SOLVE-SMARTย I learned something new too. Great way to start the day. @Nine Thanks for another method. I always have a hard time wrapping my head around regexp so it's always good to see a simple example I can learn from.
RTFC Posted December 22, 2022 Posted December 22, 2022 Thanks. @TimRude: it's fast too (one op each in assembly), and easily adaptable to handle any desired set of consecutive bits (not just whole nibbles = 1 char in hex -string notation = 4 bits). You may wish to check out the other Bitwise operators (NOT, OR, XOR, Rotate) in the Helpfile as well. @SOLVE-SMART: if you wish to learn more about the power of bitmasks, here's something to chew on. My Contributions and Wrappers Spoiler BitMaskSudokuSolverย BuildPartitionTable ย CodeCrypter ย CodeScanner ย DigitalDisplay ย Eigen4AutoIt ย FAT Suite ย ย HighMem ย MetaCodeFileLibrary ย OSgrid ย Pool ย RdRand ย SecondDesktop ย SimulatedAnnealing ย Xbase I/O
SOLVE-SMART Posted December 22, 2022 Posted December 22, 2022 Hiย @RTFC, 25 minutes ago, RTFC said: [...] if you wish to learn more about the power of bitmasks, here's something to chew on. thank you, I will have a look ๐ .Best regardsSven________________Stay innovative! ==> AutoIt related: ๐ GitHub, ๐ Discord Server Spoiler ๐ย Au3Forums ๐ฒ AutoIt (en) Cheat Sheet ๐ AutoIt limits/defaults ๐ Code Katas: [...] (comming soon) ๐ญ Collection of GitHub users with AutoIt projects ๐ย False-Positives ๐ฎย Me on GitHub ๐ฌย Opinion about new forum sub category ๐ย UDF wiki list โย VSCode-AutoItSnippets ๐ย WebDriver FAQs ๐จโ๐ซย WebDriver Tutorial (coming soon)
TimRude Posted December 23, 2022 Author Posted December 23, 2022 I just came across the _WinAPI_HiWord and _WinAPI_LoWord functions in the WinAPIConv.au3 include file: ; #FUNCTION# ==================================================================================================================== ; Author ........: Paul Campbell (PaulIA) ; Modified.......: ; =============================================================================================================================== Func _WinAPI_HiWord($iLong) Return BitShift($iLong, 16) EndFunc ;==>_WinAPI_HiWord ; #FUNCTION# ==================================================================================================================== ; Author ........: Paul Campbell (PaulIA) ; Modified.......: ; =============================================================================================================================== Func _WinAPI_LoWord($iLong) Return BitAND($iLong, 0xFFFF) EndFunc ;==>_WinAPI_LoWord Wrappers for the exact same method. ๐
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now