TimRude Posted December 22, 2022 Share 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? Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted December 22, 2022 Share 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! Stay innovative! 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) Link to comment Share on other sites More sharing options...
Solution RTFC Posted December 22, 2022 Solution Share 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 Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted December 22, 2022 Share 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! Stay innovative! 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) Link to comment Share on other sites More sharing options...
Nine Posted December 22, 2022 Share 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) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
TimRude Posted December 22, 2022 Author Share 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. Link to comment Share on other sites More sharing options...
RTFC Posted December 22, 2022 Share 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 Link to comment Share on other sites More sharing options...
SOLVE-SMART Posted December 22, 2022 Share 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! Stay innovative! 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) Link to comment Share on other sites More sharing options...
TimRude Posted December 23, 2022 Author Share 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. 😁 Link to comment Share on other sites More sharing options...
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