AlwaysQuestioning
AlwaysQuestioning

Reputation: 1484

How can I treat a string as a sequence of hexadecimal values in PowerShell?

I have a string of characters in PowerShell like so:

Encoded: A35C454A

I want to treat each character as a hexadecimal value. In Ruby, this is as simple as Encoded[0].hex. How can I do this in PowerShell?

Upvotes: 1

Views: 9106

Answers (2)

Chandan Rai
Chandan Rai

Reputation: 10377

please have a look

function asciiToHex($a)
{
$b = $a.ToCharArray();
Foreach ($element in $b) {$c = $c + "%#x" + [System.String]::Format("{0:X}",
[System.Convert]::ToUInt32($element)) + ";"}
$c
}

http://learningpcs.blogspot.in/2009/07/powershell-string-to-hex-or-whatever.html

Upvotes: -1

Simon Catlin
Simon Catlin

Reputation: 2229

Simples:

[Convert]::ToInt32($encoded[0], 16);

(ToInt16 can be used too, but the built-in Int type is actually a shorthand for Int32)

Upvotes: 5

Related Questions