JavaScript Base64-Encoding Binary Data in IDM (a.k.a using Java and JavaScript within the IDM engine)


Base64-encoding data is found in places all over the IT world and in home users' systems as well. Being able to encode data in Java or ECMAScript/JavaScript can be valuable when those are the languages available to you. This sample shows how to convert from one format to another and eventually encode data for use within a directory like eDirectory.

In a recent article I mentioned the possibilities of ECMAScript/Javascript within Java for scripting within Java applications or just for your own system. While working on an issue recently the need to convert a string of characters representing binary to a Base64-encoded representation of that binary value was manifest. As the issue was for use within Novell Identity Manager the following was able to be used to accept the string of binary and then convert it to a valid Base64-encoded value. The same could be done within any application with access to the com.novell.xml.util package (or a suitable replacement to do the Base64-encoding) and a Rhino implementation to run the rest of the conversion code from string to binary. ECMAScript/JavaScript Development Without a Web Browser

<code interpreter="rhino">
function b64encbinstring(binstring) {
var mylong0 = java.lang.Long.parseLong(binstring, 2);
var bos = new;
var dos = new;
var bytedata = bos.toByteArray();
var base64c = new;
var base64string=new, (8-Math.ceil(binstring.length/8)), Math.ceil(binstring.length/8), false));
return base64string;

b64encbinstring('00101010110111100001010101000101101'); //Line to Base64-encode a string of zeros and ones and return the appropriate string.

This may not be the most-elegant method of doing what is needed but it was something that otherwise would have required custom Java code to do the same compiled into the application in one way or another. If somebody can condense the code above to make is simpler or avoid unnecessary steps please feel free to do so in the comments section.


How To-Best Practice
Comment List