BASE64 Conversion

Please do not use to report errors- use your regional help desk.
Please mark posts as being for RPG or RDMLX (LANSA) developer.
To subscribe by email, display this forum, scroll to the end and select ‘Subscribe Forum’.
Post Reply
MarcusLancaster
Posts: 44
Joined: Tue Nov 05, 2013 9:28 am

BASE64 Conversion

Post by MarcusLancaster » Mon Oct 15, 2018 11:19 am

Hi all.

Quick question - is there a way to convert to/from BASE64 in client side LR javascript?

Cheers for now.

Marcus.

jasonzhou
Posts: 104
Joined: Tue Jan 10, 2017 11:26 pm

Re: BASE64 Conversion

Post by jasonzhou » Mon Oct 15, 2018 9:14 pm

There are two JavaScript functions btoa() and atob() can do BASE64 encoding/decoding. (https://developer.mozilla.org/en-US/doc ... d_decoding)

For exmaple:
var encodedData = window.btoa('Hello, world'); // encode a string
var decodedData = window.atob(encodedData); // decode the string

MarcusLancaster
Posts: 44
Joined: Tue Nov 05, 2013 9:28 am

Re: BASE64 Conversion

Post by MarcusLancaster » Tue Oct 16, 2018 6:06 am

Hi Jason.

Thanks for this - I'll give it a try.

Cheers.

Marcus.

jasonzhou
Posts: 104
Joined: Tue Jan 10, 2017 11:26 pm

Re: BASE64 Conversion

Post by jasonzhou » Thu Oct 18, 2018 9:56 pm

atob and btoa may not work if there is no JavaScript window context, so the following code would satisfy the requirement.
  • Encode byte array

Code: Select all

function base64EncodeByteArray(byteArray)
{
    const BASE64CODE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    var result = "";
    var restBytes = byteArray.length % 3;
    var byteBlocks = byteArray.length / 3;
    var index = 0;
    
    if (byteBlocks >= 1)
    {
        for (var i = 0; i < byteBlocks; i++)
        {        
            var block = (byteArray[index++] << 16) | (byteArray[index++] << 8) | byteArray[index++];
            result += BASE64CODE[(block&0xFC0000)>>18] + BASE64CODE[(block&0x3F000)>>12] + BASE64CODE[(block&0xFC0)>>6] + BASE64CODE[(block&0x3F)];
        }
    }

    //
    var remainingBytes = byteArray.length - index;
    
    switch(remainingBytes)
    {        
        case 1:
            var block = byteArray[index]
            result += BASE64CODE[(block&0xFC)>>2] + BASE64CODE[(block&0x03)<<4] + "==";
            break;
            
        case 2:
            var block = (byteArray[index] << 8) | byteArray[index+1];
            result += BASE64CODE[(block&0xFC00)>>10] + BASE64CODE[(block&0x03F0)>>4] + BASE64CODE[(block&0x0F)<<2] + "=";
            break;
    }
    
    //
    return result;
}
  • Encode binary data read from SQLite database with hex() function

Code: Select all

function hexStrToBase64(hexString)
{
    if (hexString.length % 2 == 0)
    {
        var bufferIndex = 0;
        var buffer = new Uint8Array(hexString.length/2);

        for(var i=0;i<hexString.length;i+=2)
        {
            var c1 = hexString.charAt(i);
            var c2 = hexString.charAt(i+1);
            var i1 = parseInt(c1,16);
            var i2 = parseInt(c2,16);
            var data = i1<<4 | i2;
            buffer[bufferIndex++] = data;
        }
    }
    
    var result = base64EncodeByteArray(buffer);
    return result;
}

MarcusLancaster
Posts: 44
Joined: Tue Nov 05, 2013 9:28 am

Re: BASE64 Conversion

Post by MarcusLancaster » Mon Oct 22, 2018 2:03 am

Excellent - thanks for the follow-up post Jason

Post Reply