Top Answer
PeterVandivier
## PowerShell Core
#### [Hex to B64]
```powershell
#!/usr/bin/env pwsh
<#
.DESCRIPTION
Hex String to Base64 String
.NOTES
Copied largely from https://stackoverflow.com/a/43776131/4709762
#>
Param(
[string]
$HexString
)
$TextArray = -split ($HexString -replace '..', '0x$& ')
$ByteArray = $TextArray -as [byte[]]
$Base64String = [System.Convert]::ToBase64String($ByteArray)
Write-Host $Base64String
```
`$HexString -replace '..', '0x$& '` splits our string into 2 character chunks. It prefixes the `0x` identifier and suffixes a space character. `DEADBEEF` becomes `0xDE OxAD OxBE OxEF`.
`-split` acts on the space character by default and changes our string into an array which we can then cast as raw `[byte[]]`s before feeding it into the base64 conversion.
The PowerShell verbosity you know and love^(?)^ is here in [System.Convert.ToBase64String]. Into this utility we feed a byte array to the dotnet builtin.
#### [B64 to Hex]
```powershell
#!/usr/bin/env pwsh
<#
.DESCRIPTION
base64 string to hex string
#>
Param(
[string]
$Base64String
)
$ByteArray = [System.Convert]::FromBase64String($Base64String)
$HexObject = $ByteArray | Format-Hex
if($PSVersionTable.PSVersion.Major -ge 7){
$HexString = $HexObject.HexBytes -replace ' '
}else{
$HexString = ($HexObject.Bytes | ForEach-Object {"{0:X}" -f $_}) -join ''
}
Write-Host $HexString
```
The verbosity you know and love^(?)^ is exposed in [System.Convert.FromBase64String] but you also get to play with a favourite newer function of mine, [Format-Hex], which does the conversion to `[byte[]]` but provides additional properties & methods.
Sadly at this time it looks like the `.HexBytes` attribute of `Format-Hex` may only be available in PowerShell 7 (TIO is currently on 6). If you're accessing `.HexBytes` you've already got a string and you need to strip out spaces. If you're accessing `.Bytes` , you'll need to `-f` format each byte-to-text and `-join` the resulting array.
[System.Convert.ToBase64String]: https://docs.microsoft.com/en-us/dotnet/api/system.convert.tobase64string
[System.Convert.FromBase64String]: https://docs.microsoft.com/en-us/dotnet/api/system.convert.frombase64string
[Format-Hex]: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/format-hex
[Hex to B64]: https://tio.run/##VY9Nb4JAEIbv@yum0RRNCquI0JrapFoTvagpJD0QDisdlHRhye5W5dfjhthoZy7z8c4zM5U4oVQH5NxOhcSm6TzQXyXpLi8plkeoTupAXjvE@ViE88/VNlpt1gSMLfEMoZZ5uQctYMYU@t61QJz1JlqErWwuqhy/gTO5R15DJkUBB60rNaFUaZb@iCPKjIuTk4qCMuqNgsAfjobUCwYvge@SzhvZMsmKXouLVbshaZOuueG6sU@6EZ71u5SshinYquK5ht5NAbbEirMUwXIc6wmswbn7CJaZm9Ua/@buIDZTEO9ML04SMKr2wStrCnFYK42FMxeluV8nk0kk7iW9G7ZPvmSu0V4KpeEfhzRN4z37Yz81nrmDceBngWti7wI "Hex to B64 in PowerShell Core – TIO"
[B64 to Hex]: https://tio.run/##bY9fa8IwFMXf8ynutNCWkTq2MUHmYDpFHzZllW0gMtJytZW2cTeZf@j62bvainSwPOXc3N85Jxu5Q1IBRhH3JWGeNy9a34paXpi0MNnCZqcCdt9kztPA7b@Op7Px5IVBcTyh8O4WlKYwWYGWEOD@pFjzgU0FidgqN@fVdFEKo1dybrVoM6N30PhIJA7Qhbl7UBpjpy@TLZJedDpDknGdsP7wNjBjhPuJt0ZfF3zN7AeGkmKhefHOwqVlTN234qOhTGbCi9A5S@dZrCUBXyG07bTqWDBVwNHzHOAUt2OAAk64iYSPYILJMowU/gNaNbLCyk4D4Qf81DhtpFedj6wBfAnGZ2YDX8swAdMElrF3CjXykVS65pvn@c3X9WV71@3@Ag "B64 to Hex in PowerShell Core – TIO"
Answer #2
PeterVandivier
## Python 3
#### [Hex to B64](https://tio.run/##VU/BboQgEL3zFTR7AJKNNluXbZp46B/0Dyao6HoQCeCm/Xo6KFstJMzMm8fMe/Yn3GfzFuPppVy8K5vRlNo8qN1wQsbJzi7Qdu5065@VcoNVzmtC1uBo/QcVn25YJm3C19rhIlMK1XWgco@zu/4GH9xoBiYI6XRPExJmaGTF96b4IBQPghnATZuUQpuU8Fzhk6r955mmHUxgbJTXsmLiSRLrTKfD4sxhNHrtKYBRkwagdU0ZwKRGA8A2Faje4/7sZw3Jkc8D/4k8uEmU4mBpJVtMA9@/iBhj9S6vssXbX16vN9nfLphXvw "Hex to B64 in Python 3 – TIO")
Copied largely from [SO][2]. Uses the [codecs] library with 2 pretty straightforward functions
> codecs.encode(obj, encoding='utf-8', errors='strict')
> codecs.decode(obj, encoding='utf-8', errors='strict')
The `hex` and `base64` options are described in the [binary-transforms] section of the documentation.
```python
#!/usr/bin/env python3
import codecs
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('hex_string')
def hex_to_b64(hex_string):
b64_string = codecs.encode(codecs.decode(hex_string, 'hex'), 'base64').decode()
return b64_string
if __name__ == '__main__':
args = parser.parse_args()
b64_string = hex_to_b64(args.hex_string)
print(b64_string)
```
#### [B64 to Hex](https://tio.run/##VY8xb8MgEIV3fgVVBpsFS20UtZU8dPKaqlLWE5SL48EYHbhqfj3FmNYuy8G79@4@3D3cJvsU4@GhmT01erAN2i/uVp2xYXQTBa6Vx9Px96Wod4o8MpYL8fZPkm/UzyPacM6dWhSLVMaAKr260qcj@ECD7SvBmMErX5QwwQ2/660pXhlPJ4lFSJtWFJlMBj8ng3u7XOIiZwjDTHYXTX@5cgCrRgTgbcsrgFENFqBatyQ6n@YX3lwWYl8G/ofYaBeL3DFks0vXUG8REWP86C5ed8/95fHlrrv39gc "B64 to Hex in Python 3 – TIO")
Copied largely from [SO][1]. Uses the [base64] library and the [.hex()] builtin function (>= python 3).
```python
#!/usr/bin/env python3
import base64
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('b64_string')
def b64_to_hex(b64_string):
hex_string = base64.b64decode(b64_string).hex()
return hex_string
if __name__ == '__main__':
args = parser.parse_args()
hex_string = b64_to_hex(args.b64_string)
print(hex_string)
```
[1]: https://stackoverflow.com/a/47805853/4709762
[2]: https://stackoverflow.com/a/42230475/4709762
[codecs]: https://docs.python.org/3/library/codecs.html
[binary-transforms]: https://docs.python.org/3/library/codecs.html#binary-transforms
[base64]: https://docs.python.org/3/library/base64.html
[.hex()]: https://docs.python.org/3/library/functions.html#hex