package railo.commons.lang;

import railo.runtime.exp.ExpressionException;

public class NumberUtil {
	public static int hexToInt(String s, int defaultValue) {
		try {
			return hexToInt(s);
		} catch (ExpressionException e) {
			return defaultValue;
		}
	}
	public static int hexToInt(String s) throws ExpressionException {
        int[] n = new int[s.length()];
        char c;
        int sum = 0;
        int koef = 1;
        for(int i=n.length-1; i>=0; i--) {
            c = s.charAt(i);
            
            if(!((c>='0' && c<='9') || (c>='a' && c<='f'))) {
            	throw new ExpressionException("invalid hex constant ["+c+"], hex constants are [0-9,a-f]");
            }
            	
            //System.out.println(c);
            switch (c) {
                case 48:
                    n[i] = 0;
                    break;
                case 49:
                    n[i] = 1;
                    break;
                case 50:
                    n[i] = 2;
                    break;
                case 51:
                    n[i] = 3;
                    break;
                case 52:
                    n[i] = 4;
                    break;
                case 53:
                    n[i] = 5;
                    break;
                case 54:
                    n[i] = 6;
                    break;
                case 55:
                    n[i] = 7;
                    break;
                case 56:
                    n[i] = 8;
                    break;
                case 57:
                    n[i] = 9;
                    break;                      
                case 97:
                    n[i] = 10;
                    break;
                case 98:
                    n[i] = 11;
                    break;
                case 99:
                    n[i] = 12;
                    break;
                case 100:
                    n[i] = 13;
                    break;
                case 101:
                    n[i] = 14;
                    break;
                case 102:
                    n[i] = 15;
                    break;
            }
            
            sum = sum + n[i]*koef;
            koef=koef*16;
        }
        return sum;
    } 
}

@@ -2224,12 +2224,360 @@
 m;%0A    %7D
- 
+%0A%09public static byte%5B%5D longToByteArray(long l) %7B%0A%09%09byte%5B%5D ba=new byte%5B8%5D;%0A%09%09for(int i=0; i%3C64; i+=8) %7B%0A%09%09%09ba%5Bi%3E%3E3%5D = new Long((l&(255L%3C%3Ci))%3E%3Ei).byteValue();%0A%09%09%7D%0A%09%09return ba;%0A%09%7D%0A%09%0A%09public static long byteArrayToLong(byte%5B%5D ba)%7B%0A%09%09long l=0;%0A%09%09for(int i=0; (i%3Cba.length)&&(i%3C8); i++) %7B%0A%09%09%09l %7C= (((long)ba%5Bi%5D)%3C%3C(i%3C%3C3))&(255L%3C%3C(i%3C%3C3));%0A%09%09%7D%0A%09%09return l;%0A%09%7D
 %0A%7D%0A

add a long2bytearray converter method to NumberUtil, usd to add date information to class files.