Java tutorial
//package com.java2s; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlSerializer; public class Main { public static final void writeByteArrayXml(byte[] val, String name, XmlSerializer out) throws XmlPullParserException, java.io.IOException { if (val == null) { out.startTag(null, "null"); out.endTag(null, "null"); return; } out.startTag(null, "byte-array"); if (name != null) { out.attribute(null, "name", name); } final int N = val.length; out.attribute(null, "num", Integer.toString(N)); StringBuilder sb = new StringBuilder(val.length * 2); for (int i = 0; i < N; i++) { int b = val[i]; int h = b >> 4; sb.append(h >= 10 ? 'a' + h - 10 : '0' + h); h = b & 0xff; sb.append(h >= 10 ? 'a' + h - 10 : '0' + h); } out.text(sb.toString()); out.endTag(null, "byte-array"); } }