bdev053019
bdev053019

Reputation: 5

Convert a string of ones and zeros to hex

I'm looking for a simple way to convert a string of 1's and 0's to its hex equivalent.

[string]$bintest = "1100110101011111"
[long]$testnumber = [System.Convert]::ToInt32($bintest,2)
"{0:x4}" -f $testnumber

The code above works as needed but it seems like too many steps. Is there a way to avoid converting the string to decimal before convering it to hex?

Upvotes: 0

Views: 225

Answers (1)

the shortest answer I could create

'{0:x}' -f [Convert]::ToInt32('1100110101011111',2)

Hope it helps

Edit: As I refreshed I saw the comment of @Ansgar Wiechers, two persons one idea I guess.

Upvotes: 2

Related Questions