+
+ /**
+ * Separates a string based on a separator, taking into account an escape character:
+ * <code>seperateString("a:/::b", ":", "/");</code> will lead to
+ * a List with 3 Strings: <code>"a"</code>, <code>":"</code> and <code>"b"</code>
+ *
+ * @param aString The string to split
+ * @param aSeparator
+ * @return
+ */
+
+ public static List splitStringWithEscape(String aString, char aSeparator, char anEscape) {
+ List result= new ArrayList();
+ int previousPosition = 0;
+ int position;
+
+ StringBuffer currentItem = new StringBuffer();
+
+ if (aString!=null && aString.length()>0) {
+ while ((position = indexOfCharacters(aString, new char[] {aSeparator, anEscape}, previousPosition))>=0) {
+ currentItem.append(aString.substring(previousPosition, position));
+
+ if (aString.charAt(position)==aSeparator) {
+ result.add(currentItem.toString());
+ currentItem.delete(0, currentItem.length());
+ }
+ else {
+ if (aString.length()>position+1) {
+ position=position+1;
+ currentItem.append(aString.charAt(position));
+ }
+ else {
+ currentItem.append(aString.charAt(position));
+ }
+ }
+ previousPosition = position + 1;
+ }
+ currentItem.append(aString.substring(previousPosition, aString.length()));
+ result.add(currentItem.toString());
+ }
+
+ return result;
+ }
+
+ public static String replicateString(String aString, int aCount) {
+ StringBuffer result = new StringBuffer();
+
+ for (int i=0; i<aCount; i++)
+ result.append(aString);
+
+ return result.toString();
+ }
+
+ public static String replicateChar(char aCharacter, int aCount) {
+ char result[] = new char[aCount];
+
+ for (int i=0; i<aCount; i++)
+ result[i]= aCharacter;
+
+ return new String(result);
+ }
+
+ public static String padStringLeft(String aString, int aLength, char aPadCharacter) {
+ if (aString.length()<aLength)
+ return replicateChar(aPadCharacter, aLength-aString.length()) + aString;
+ return aString;
+ }
+
+ private static final char HEX_CHARACTERS[] = {
+ '0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
+ };
+
+ public static String convertToHex(long aData, int aNumberOfDigits) {
+ StringBuffer result = new StringBuffer();
+
+ for (int digit = aNumberOfDigits-1; digit>=0; digit--) {
+ int value = (int) (aData >> (digit*4)) & 0xf;
+ result.append(HEX_CHARACTERS[value]);
+ }
+
+ return result.toString();
+ }
+
+ public static String convertToHex(byte[] aData) {
+ StringBuffer result = new StringBuffer();
+
+ for (int i = 0; i<aData.length; i++) {
+ result.append(convertToHex(aData[i], 2));
+
+ }
+
+ return result.toString();
+ }
+}