Sample code for 30+ languages & platforms
Java

Append Encoded Binary Data to StringBuilder

Demonstrates how to append encoded binary data to the contenets of a StringBuilder.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        BinData bd = new BinData();

        try {
            bd.loadFile("qa_data/jpg/starfish.jpg");
        } catch (ChilkatException e) {
            System.out.println("Failed to load file.");
            return;
        }

        // For example, let's say we want construct simple JSON containing the base64 representation of the above JPG file.
        StringBuilder sb = new StringBuilder();
        sb.append("{ \"jpg\": \"");
        // GetEncodedSb appends the enocded representation of the binary data to the StringBuiler passed in the 2nd arg.
        bd.getEncodedSb("base64", sb);
        sb.append("\" }");

        System.out.println(sb.getAsString());

        // Output looks like this:
        //  { "jpg": "/9j/4AAQSkZJRgABAg...rcQ+vo//2Q==" }
    }
}