- D:\scripts> [System.Text.Encoding]::Unicode.GetBytes('12345')|%{[System.Convert]::ToString($_,16)
- 31
- 0
- 32
- 0
- 33
- 0
- 34
- 0
- 35
- 0
Convert string to hex for registry
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Convert string to hex for registry
Considering you want to store this in the registry then this is all you need to do.
$bytes = [System.Text.Encoding]::Unicode.GetBytes('12345')
New-ItemProperty -PAth HKCU:\SOFTWARE\TestKey1 -Name Test1 -Value $bytes -PropertyType BINARY
$bytes = [System.Text.Encoding]::Unicode.GetBytes('12345')
New-ItemProperty -PAth HKCU:\SOFTWARE\TestKey1 -Name Test1 -Value $bytes -PropertyType BINARY
Re: Convert string to hex for registry
Ha, thank you that's working exactly as you said. I think because I ran just the first bit without writing to the registry I assumed it would write the same as what it outputted. Obviously after writing to the registry it is correct.
Thanks again!
Thanks again!
Re: Convert string to hex for registry
One more question, how do I overwrite the value? If I change it to set-itemproperty then "ProperyType" for BINARY isn't available
EDIT: Never mind, just used -force
EDIT: Never mind, just used -force
Re: Convert string to hex for registry
The registry does not have any dots. It is just how regedit displays an array of bytes.syscouk wrote:EDIT: When I run [System.Text.Encoding]::Unicode.GetBytes('0.1.1.')|%{[System.Convert]::ToString($_,16)} I get 30 0 2e 0 31 0 2e 0 31 0 2e 0 which is wrong for the printer
To see the raw value use REG.
reg query HKCU\Software\TestKey1 /v test1
Re: Convert string to hex for registry
Fair enough, thanks again worked a treat once I'd got over my own understanding!
Re: Convert string to hex for registry
Note that you can also just do this:
reg add HKCU\Test /v test1 /t REG_BINARY /d 003000310031 /f
Adding a property to the registry will also create the key if it does not exist.
reg add HKCU\Test /v test1 /t REG_BINARY /d 003000310031 /f
Adding a property to the registry will also create the key if it does not exist.