MiniM. Saving time.

| About | Download | Tools | Knowledge Base | How to Buy |

MiniM Overview

Getting Started




Cache Tools

GT.M Tools

Knowledge Base




MiniM Knowledge Base | RUS | Jul 6, 2010

How to Encode Byte Sequence to BASE64.

Перевод на русский

When transferring data used open standard formats for encoding. One of the most frequently used is a format code BASE64. Let's see how this encoding can be done in MUMPS.

To encode format BASE64 original sequence of bytes is divided into sets by three bytes, which are encoded as a number. The first byte is highest byte of number, the second medium and third is lowest. Then this number consider 4 groups of bits sets by the 6 bits. Each set of bits is the in dex of symbol and this symbol is outputs.

From 3 source bytes we create 4 bytes, and output byte sequence can be encoded using 6-bit alphabet. As base64 alphabet uses character sequence "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".

If last set of 3 bytes of source sequence is not full (1 or 2 bytes), output sequence padded by special terminators, 2 or 1 character "=" correspondingly.

The BASE64 specification is described in documents RFC 1421 and RFC 2045.

The output text sequence generally formats by character group by 64 (or other) characters. To make this, source byte sequence splits by group by 64/4*3=48 bytes.

MiniM Database Server implements internal $z function $zconvert with "base64" output option. This can be used to convert to BASE64 and back.

Using $zconvert this can be done as:

 q $zcvt(str,"O","base64")

The same encoding using standard MUMPS without extended $z functions can be done, for example as:

 n ret,i,num,n,part,enc,arr
 s arr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 s ret=""
 ; step up to 3 bytes
 f i=1:3:$l(str) d
 . ; get part and calculate depending of part's length
 . s part=$e(str,i,i+2)
 . ; initial accumulators
 . s enc="",num=0
 . ; calculate total number
 . f n=1:1:$l(part) s num=num*256+$a(part,n)
 . f n=n+1:1:3 s num=num*256
 . ; calculate padding symbols
 . f n=1:1:3-$l(part) s enc=enc_"=" s num=num\64
 . ; calculate encoded bytes
 . f n=1:1:$l(part)+1 s enc=$e(arr,num#64+1)_enc s num=num\64
 . s ret=ret_enc
 q ret

To encode source byte sequence, for example, global value or device input, programmer can add appropriate cycle to get source by 48 bytes and format output sequence as need

Eugene Karataev

Copyright (C) 2017 Eugene Karataev
Emails: Info Tech Support