Question
Yone Moreno · Aug 5, 2021

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?

$ZV: Cache for UNIX (Red Hat Enterprise Linux for x86-64) 2017.2.1 (Build 801_3_18358U) Tue Jul 24 2018 16:36:10 EDT
00
1 0 5 349
Log in or sign up to continue

Hey Yone.

In the first part of your question, you're converting the hexadecimal to Ascii and then attempting to convert the Ascii string to Base64 (giving you "TFgDBAEBAgF5wyYjMTQ1Oz7DunxMWAcIAQEBAhgkWE1M").

The reason you're getting a different result on the Base64 encoding on ObjectScript is because you're encoding the hexadecimal string "4C5803040101020179C3913EC3BA7C4C580708010101021824584D4C" into Base64 without converting it to Ascii first. If you try the hexadecimal string in an online Base64 encoder, you'll see the same output:

Thanks

First of all, if you want to transform a hex string into base64 (or whatever), then first you have to say, WHAT IS that hex string?

According to your example

set hexString = "4C5803040101020179C3913EC3BA7C4C580708010101021824584D4C"

the string has 56 hex chars, so if you decode those 56 hex chars, the resulting string could be:
a) 28 eigth bit characters or
b) 14 sixteen bit chars or even
c)  7 characters, each 32 bits wide.

For cases b) and c) you also have to define the endianness (big- or little-endian).

Second, I assume, your hex-string represents 8-bit chars, so we get 28 characters after converting the hex-chars into a string.

Converting to base64 means, you get for every 3 (8bit) chars four printable (8bit) chars.

We add two padding chars to the 28 byte string, so we have 30 chars,  now which are divisible by 3. This gives you  40 base64 encoded characters. But your Base64 encoding has 44 characters, which must be wrong.

Here is a simple and working solution:

Class DC.Util Extends %RegisteredObject
{
/// Hex to Base64
ClassMethod HexToB64(hex)
{
   if $length(hex)#2 zt "ELEN" // trap, two hex chars should make up each byte
   set str=""
   for i=1:2:$length(hex) set str=str_$char($zhex($extract(hex,i,i+1)))
   quit $system.Encryption.Base64Encode(str,1)
}
/// Base64 to Hex
ClassMethod B64ToHex(b64)
{
   set str=$system.Encryption.Base64Decode(b64), hex=""
   for i=1:1:$length(str) set hex=hex_$extract($zhex($ascii(str,i)+256),2,3)
   quit hex
}
}

and a short test

set hexString = "4C5803040101020179C3913EC3BA7C4C580708010101021824584D4C"
set b64String=##class(DC.Util).HexToB64(hexString)
write b64String, !, ##class(DC.Util).B64ToHex(b64String), !,hexString

TFgDBAEBAgF5w5E+w7p8TFgHCAEBAQIYJFhNTA==
4C5803040101020179C3913EC3BA7C4C580708010101021824584D4C
4C5803040101020179C3913EC3BA7C4C580708010101021824584D4C

Thanks Julius Kavay for your reply

Your contribution has helped us a lot, thanks, sincerely thanks.

Specially we thank you for your explanation it was very helpful!