Skip to content

Instantly share code, notes, and snippets.

@ivang7
Last active March 10, 2019 18:06
Show Gist options
  • Save ivang7/5710705da28bd22a5d53f55f57294ad9 to your computer and use it in GitHub Desktop.
Save ivang7/5710705da28bd22a5d53f55f57294ad9 to your computer and use it in GitHub Desktop.
#from HEX to FIle
$z = "0x1F, 0x8B, 0x08, 0x08"
$y = $z -replace " |,|0x|`n|`r", ""
Convert-HexToByteArray($y) | Set-Content template_default.html.gz -Encoding Byte
#from file to HEX
$y = Get-Content template_default.html.gz -Encoding Byte
Convert-ByteArrayToHex($y)
$y.Length
#https://www.reddit.com/r/PowerShell/comments/5rhjsy/hex_to_byte_array_and_back/
Function Convert-HexToByteArray {
[cmdletbinding()]
param(
[parameter(Mandatory=$true)]
[String]
$HexString
)
$Bytes = [byte[]]::new($HexString.Length / 2)
For($i=0; $i -lt $HexString.Length; $i+=2){
$Bytes[$i/2] = [convert]::ToByte($HexString.Substring($i, 2), 16)
}
$Bytes
}
Function Convert-ByteArrayToHex {
[cmdletbinding()]
param(
[parameter(Mandatory=$true)]
[Byte[]]
$Bytes
)
$HexString = [System.Text.StringBuilder]::new();
$i=0;
ForEach($byte in $Bytes){
$HexString.AppendFormat("0x{0:x2}, ", $byte) | Out-Null
$i++;
if($i -eq 15){
$i=0;
$HexString.Append("`r`n") | Out-Null
}
}
$HexString.ToString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment