Convert hexadecimal to base64
Hello, first of all thanks for your time reading our question 💭
We would need to be able to transform a hexadecimal string to a base64 coded one. 🔄
To be specific, our use case is this:
1º We have our hexadecimal as follows:
4C5803040101020179C3913EC3BA7C4C580708010101021824584D4C
2º We convert it to ASCII:
LXyÑ>ú|LX$XML
using this online tool:
https://www.binaryhexconverter.com/hex-to-ascii-text-converter
3º Then we should be able to translate the ASCII to base64:
TFgDBAEBAgF5wyYjMTQ1Oz7DunxMWAcIAQEBAhgkWE1M
We are being helped by this useful web:
https://www.motobit.com/util/base64-decoder-encoder.asp
How could we convert the hexadecimal to base64?
We have tried to use:
set cabeceraPrincipalHex = "4C5803040101020179C3913EC3BA7C4C580708010101021824584D4C"
$$$LOGWARNING("cabeceraPrincipalHex: "_cabeceraPrincipalHex)set cabeceraPrincipalBase64 = $system.Encryption.Base64Encode(cabeceraPrincipalHex)
$$$LOGWARNING("cabeceraPrincipalBase64: "_cabeceraPrincipalBase64)
It outputs the hexadecimal as it is:
cabeceraPrincipalHex: 4C5803040101020179C3913EC3BA7C4C580708010101021824584D4C
However the base64 is strange:
cabeceraPrincipalBase64: NEM1ODAzMDQwMTAxMDIwMTc5QzM5MTNFQzNCQTdDNEM1ODA3MDgwMTAxMDEwMjE4MjQ1ODRENEM=
Because it should be:
TFgDBAEBAgF5wyYjMTQ1Oz7DunxMWAcIAQEBAhgkWE1M
How would you recommend us to convert a hexadecimal to base64? ⬅️
We have also read:
https://community.intersystems.com/post/create-function-using-ascii
https://community.intersystems.com/post/there-function-convert-string-hex
▶️▶️▶️ Thanks for your time reading this question and we would be thankful if you could reply and point us to further documentation, examples, or guides.
In addition we have seen $system.encription.Base64Encode documentation:
This method performs Base64 encoding.
Use with Base64Decode.
(See RFC 4648 for more information.)
Input parameter:
Text - String to be encoded
Flags - 0 - Insert CR/LF after every 76 characters (Default)
Flags - 1 - Do not insert CR/LF after every 76 characters.
Return value: Encoded string.
Note: Base 64 encoding is not able to encode a string which contains unicode (2 byte) characters. If you need
to Base 64 encode an unicode string, you should first translate the string to UTF8 format, then encode it.
s BinaryText=$ZCONVERT(UnicodeText,"O","UTF8")
s Base64Encoded=$system.Encryption.Base64Encode(BinaryText)
Now to Decode it:
s BinaryText=$system.Encryption.Base64Decode(Base64Encoded)
s UnicodeText=$ZCONVERT(BinaryText,"I","UTF8")
How would you convert hexadecimal to base64?