Last active
January 31, 2025 14:28
-
-
Save MichalBrylka/f9f7e43d6d4f86b23d0860410723081a to your computer and use it in GitHub Desktop.
Encode Hex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
This script provides three modes of operation: | |
1. Converts a given string to its hexadecimal representation. | |
2. Decodes a given hexadecimal string back to its original UTF-8 representation. | |
3. Renames files in a given directory by encoding the part of the filename before the first '.' or '_'. | |
.PARAMETER InputString | |
A string that will be converted to hexadecimal encoding. | |
.PARAMETER EncodedString | |
A hexadecimal encoded string that will be decoded back to its original UTF-8 representation. | |
.PARAMETER Directory | |
The directory containing files to be renamed. | |
.PARAMETER Pattern | |
A wildcard pattern to filter files for renaming. Defaults to '*'. | |
.PARAMETER WhatIf | |
If specified, renaming will not be performed but the renaming actions will be logged. | |
.DESCRIPTION | |
If an InputString is provided, it is converted to hexadecimal encoding and outputted. | |
If an EncodedString is provided, it is decoded from hexadecimal and outputted in its UTF-8 representation. | |
If a Directory is provided, files matching the specified pattern are renamed, encoding the portion of the filename before the first '.' or '_'. | |
If -WhatIf is specified, renaming will not be performed but the changes will be logged. | |
.EXAMPLE | |
.\Encode-Hex.ps1 -InputString "Hello, World!" | |
Outputs the hexadecimal encoded representation of "Hello, World!". | |
.EXAMPLE | |
.\Encode-Hex.ps1 -EncodedString "48656C6C6F2C20576F726C6421" | |
Outputs the decoded UTF-8 representation of "Hello, World!". | |
.EXAMPLE | |
.\Encode-Hex.ps1 -Directory "C:\path\to\directory" -Pattern "*.xls" | |
Renames all .xls files in the specified directory, encoding the first part of each filename. | |
.EXAMPLE | |
.\Encode-Hex.ps1 -Directory "C:\path\to\directory" -Pattern "*.xls" -WhatIf | |
Logs all renaming operations without actually renaming the files. | |
.EXAMPLE | |
@("apple", "banana", "cherry") | ForEach-Object { | |
[PSCustomObject]@{ | |
Original = $_ | |
Transformed = .\Encode-Hex.ps1 -InputString $_ | |
Check = .\Encode-Hex.ps1 -EncodedString (.\Encode-Hex.ps1 -InputString $_) | |
} | |
} | Format-Table -AutoSize | |
Encode array of objects and display in table | |
#> | |
param ( | |
[string]$InputString, | |
[string]$EncodedString, | |
[string]$Directory, | |
[string]$Pattern = "*", | |
[switch]$WhatIf | |
) | |
function Convert-ToHex([string]$InputString) { | |
$bytes = [System.Text.Encoding]::UTF8.GetBytes($InputString) | |
return ($bytes | ForEach-Object { "{0:X2}" -f $_ }) -join "" | |
} | |
function Convert-FromHex([string]$EncodedString) { | |
$bytes = for ($i = 0; $i -lt $EncodedString.Length; $i += 2) { [Convert]::ToByte($EncodedString.Substring($i, 2), 16) } | |
return [System.Text.Encoding]::UTF8.GetString($bytes) | |
} | |
if ($InputString) { | |
Convert-ToHex -InputString $InputString | Write-Output | |
} | |
elseif ($EncodedString) { | |
Convert-FromHex -EncodedString $EncodedString | Write-Output | |
} | |
elseif ($Directory) { | |
$currentDir = Get-Location | |
Get-ChildItem -Path $Directory -Filter $Pattern | Where-Object { !$_.PSIsContainer } | ForEach-Object { | |
if ($_.BaseName -match "^([^._]+)(.*)") { | |
$encodedPart = Convert-ToHex -InputString $matches[1] | |
$newName = $encodedPart + $matches[2] + $_.Extension | |
$newPath = Join-Path $_.DirectoryName $newName | |
$originalName = [System.IO.Path]::GetRelativePath($currentDir.Path, $_.FullName) | |
$newPathRelative = [System.IO.Path]::GetRelativePath($currentDir.Path, $newPath) | |
Write-Host ("{0,-30}" -f $originalName) -NoNewline | |
Write-Host ">>" -ForegroundColor Red -NoNewline | |
Write-Host " $newPathRelative" | |
if (-not $WhatIf) { | |
Rename-Item -Path $_.FullName -NewName $newPath | |
} | |
} | |
} | |
} | |
else { | |
Write-Output "Please provide a string to encode, a hexadecimal string to decode, or a directory path to process files." | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String hexToUtf8(String hex) { | |
if (hex.length() % 2 != 0) { | |
throw new IllegalArgumentException("Hex string must have an even length"); | |
} | |
byte[] bytes = new byte[hex.length() / 2]; | |
for (int i = 0; i < hex.length(); i += 2) { | |
bytes[i / 2] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16); | |
} | |
return new String(bytes, StandardCharsets.UTF_8); | |
} | |
public static String utf8ToHex(String utf8) { | |
StringBuilder hex = new StringBuilder(); | |
byte[] bytes = utf8.getBytes(StandardCharsets.UTF_8); | |
for (byte b : bytes) { | |
hex.append(String.format("%02X", b)); | |
} | |
return hex.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment