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

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?

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

💎 Code Katas: [...] (comming soon)

👨‍🏫 WebDriver Tutorial (coming soon)

##### 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)```

My Contributions and Wrappers

Spoiler
##### 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

💎 Code Katas: [...] (comming soon)

👨‍🏫 WebDriver Tutorial (coming soon)

##### Share on other sites

Another way :

```#include <Array.au3>

Local \$iValFull = 0x12345678
Local \$sValue = Hex(\$iValFull, 8)

Local \$aValue = StringRegExp(\$sValue, "(.{4})(.{4})", 1)
_ArrayDisplay(\$aValue)```

“They did not know it was impossible, so they did it” ― Mark Twain

Spoiler

##### Share on other sites

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

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

My Contributions and Wrappers

Spoiler
##### 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

💎 Code Katas: [...] (comming soon)

👨‍🏫 WebDriver Tutorial (coming soon)

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

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

• ### Recently Browsing   0 members

• No registered users viewing this page.
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...