Base64 Encoding

Long time no see! Today I'm here to talk about Base64 encoding and how to do it in LabVIEW.

What is Base64 and what is used for?

Base64 is a six-bit based encoding mechanism that uses only printable characters from the ASCII table (lower and upper case letters, numbers, and three more characters, usually "+/="). This means it can be represented in any textual environment, though obviously adds an overhead respect to the original code since two bits of every byte are unused.

You will find B64 mainly in mail applications (to send attachments), but lately it's been popular in URLs and HTML too. The concept is to insert small chunks of data directly as B64 text to avoid recurrent callings for files through the web. For example, you can embed an image inside your HTML or CSS file. It's not my intention to delve too deep in this aspect, but for an image tag the resulting HTML code would look like this (let's assume a JPG file):

<img src="data:image/jpeg;base64,B64DATAHERE" />

So, embedding this blog's icon would be like this:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAADmQAAA5kBQyHEuAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAJbSURBVDiNjVO/axpxFH+eem1qSOIRImdJCVlsi/RoqA5HCxcTyFCH4JJFIVIy6B8QDIJTXCRDHMRCRgNGMtZChpCc0sUmVe4qMYaLOPV6y52cDV5+9GuXWvRiad/23ufH+7zhGbrdLgQCgTmGYSiCIKwYhmHwu87PzydUVX3gdrul3gwhhGRZVliW5TKZTNng9/vnEonEW5Ik/wh7NT8/7221WpZKpZLTY6IoovX19Y8YwzDUMHGj0RgRBMHO87yjVCpN6HGSJDGGYSiMIAirHgQA2N7efp7P57Mmk+kuHo+7hnF6J9/brmkapmmaiaKoNk3TXw8PD+cURTHreUPFAABbW1uOYDBYBwDw+/18p9MZicVi1DDuUIPLy0srRVHt5eXlBZ7nJ0mSlPb3993/ZZDL5ew0TX9bXV19Y7fb1WQy+SUYDH6SJGlqZ2dn5p8GBwcHM2tra81arWYXBGESAGBjY+PMYrFcpdPpeykGDKrV6ihJkj8AAHw+H3d0dPQqEAi8xnG8u7i4WOY47unp6el4v8bU35jN5m4kEjkDAIhGo7VisVjZ3d1dKBQKz5LJ5AePx9Ocnp7u/DWBw+G4GhsbuwMAwHEcHR8f51OpVEZV1dGVlZV3S0tL3202282AAUII6e/qr3A43CiXy++NRuPPzc3Nl/0YQghhsiwrelE2m33c38/OznacTqegqurD/rksywrGsiwniuJAipOTk6l6vW7p9c1mc+Ti4uJJKBTiejNRFBHLspxh2DtfX18b9/b2Xtze3hrb7fajVqs17vV6P7tcLkn/zr8AvxgSDK5NQZkAAAAASUVORK5CYII=" />

And it looks great:

Base64 encoding

Now the big question, how do we get that B64 data? Firstly you must consider there are several implementations of Base64, and the differences are nearly always in those three extra characters we talked about previously. Some environments can't accept them because they are considered special characters, so maybe you need to escape them or use another charset. Here I'm using the standard Base64 encoding, with + and / as valid characters and = as the padding char.

Download Base64 Encode (VI for LabVIEW 2016)

The VI you can download through the previous link takes a string and encodes it as BASE64. As you can see, I've kept the code as simple and straightforward as possible. The tricky part here is to reverse the boolean arrays, since LabVIEW puts the LSB (less significant bit) first and we need them the other way around. The conversion to ASCII uses the ASCII code instead of strings, cause this way we can simplify the case structure.

If you need to modify the charset, locate a section of code near the "To ASCII" comment, values 43 (+) and 47 (/), and "Output padding", character 61 (=), and replace that numbers with the ASCII values you want. For example, URLs usually accept 45(-), 95(_) and no padding.

If we don't have raw text but a file, recover its binary data as string:

Base64 decoding

So far, so good. But what if we want to walk the opposite way? No problem, here's the reverse function, Base64 Decode.

Download Base64 Decode (VI for LabVIEW 2016)

The code is even simpler. When changing the charset, remember what we said for the encoding case, with the proviso this decoding VI ignores padding (so don't worry about that char).

Hope it's helpful, see ya 😊.

Comentarios

Entradas populares de este blog

SNMP library for LabVIEW

Retrieve CPU temperature