Jump to content

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


Go to solution Solved by RTFC,

Recommended Posts

Posted

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?

Posted

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!

==> 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
Posted
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)

ย 

My Contributions and Wrappers

Posted

Hi @RTFC,

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

Best regards
Sven

________________
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)

Posted

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

Posted

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

Posted

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!

==> 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)

Posted

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. ๐Ÿ˜

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...