Hex to Binary, encoded with Base64; understanding differing PowerShell -vs- Python results

Question:

I have a interesting puzzle. I am trying to perform the steps of Decimal to Hex, Hex to Binary representation, and then Encode to Base64. I wrote it in Python (where I am more comfortable), then tried to write it in PowerShell, and I get close, but no cigar!

Sample Code (Python):

decimalValue = 65537
print("Decimal: ", decimalValue)

# Convert to Hex bytes, and check for missing leading zero that would leave us 
# with an odd number of values making up the bytes.
hexValue = "{0:x}".format(int(decimalValue))
hexValue = "0{0}".format(hexValue) if len(hexValue) % 2 else hexValue
print("Hex: ", hexValue)

# Use Python module:  binascii to return the binary data represented by the
# hexadecimal string
binaryValue =  binascii.unhexlify(hexValue.encode("utf-8"))
print("Binary: ", binaryValue)

#Base64 encode the binary representation.
b64Version = base64.urlsafe_b64encode(binaryValue).decode('utf8').replace("=", "")
print("Base 64: ", b64Version)

Result:

Decimal:  65537
Hex:  010001
Binary:  b'x01x00x01'
Base 64:  AQAB

PowerShell Code:

$decimalValue = 65537
write-host "Decimal: ", $decimalValue

# Convert to Hex bytes, and check for missing leading zero that would leave us 
# with an odd number of values making up the bytes.

$hexValue = ('{0:X}' -f [int]$decimalValue)
$hexValue =  (($hexValue.length % 2) ? ("0"+$hexValue) : ($hexValue))
write-host "Hex: ", $hexValue

# No binascii.unhexilify equivalent in PowerShell, attempt to write my own.
$binaryValue = ($hexValue -split '(.{2})' -ne '') 
write-host "Binary: ", $binaryValue
$binaryValue = [byte[]]($hexValue -split '(.{2})' -ne '' -replace '^', '0X') 
write-host "Final Binary: ", $binaryValue
# Note PowerShell output shows values of bytes not the bytes in byte representation.

#Base64 encode the binary representation, can not get this one right?!
#If I encode the byte array.. I get:
$b64Ver =  [System.Text.Encoding]::UTF8.GetBytes($binaryValue)
write-host "Base 64: ", $b64Ver
$b64Ver = [Convert]::ToBase64String($b64Ver)
write-host "Final Base 64: ", $b64Ver

#Does not even remotely look the same.
#However, if I force PowerShell to encode each byte individually..


foreach ($aBinByte in $binaryValue) {
    write-host "Binary Byte/Base64: ", ([Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($aBinByte)))
}
# A little closer, but missing "B", and seems to be reversed?  Huh? and the "M"?

Result:

Decimal:  65537
Hex:  010001
Binary:  01 00 01
Final Binary:  1 0 1
Base 64:  49 32 48 32 49
Final Base 64:  MSAwIDE=
Binary Byte/Base64:  MQ==
Binary Byte/Base64:  MA==
Binary Byte/Base64:  MQ==

So, I learned PowerShell via MS webpages, and Google. Seemed to make sense, but I can not figure out .. am I just doing it entirely wrong? Missing steps? Why can I not replicate the Python result in Base64. HELP?

Asked By: jewettg

||

Answers:

Your $binaryValue variable already contains a byte-array representation, so you just need to pass it directly to [Convert]::ToBase64String()

# -> 'AQAB'
[Convert]::ToBase64String($binaryValue)
Answered By: mklement0
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.