Jump to content

Splitting a 8-digit hex value into 2 @ 4-digits


TimRude
 Share

Go to solution Solved by RTFC,

Recommended Posts

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

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 regards
Sven

________________
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

  • Solution
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)

 

Link to comment
Share on other sites

Hi @RTFC,

wow, that's impressive 👍 . The first new thing that I learned today (AutoIt context). Thank you.

Best regards
Sven

________________
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

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.

Link to comment
Share on other sites

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 regards
Sven

________________
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

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

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