[JAVA] hex to byte[], byte[] to hex

자바에서 헥사 스트링을 바이트 값으로 변환하는 소스

// hex to byte[]
public static byte[] hexToByteArray(String hex) {
    if (hex == null || hex.length() == 0) {
        return null;
    }

    byte[] ba = new byte[hex.length() / 2];
    for (int i = 0; i < ba.length; i++) {
        ba[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
    }
    return ba;
}

// byte[] to hex
public static String byteArrayToHex(byte[] ba) {
    if (ba == null || ba.length == 0) {
        return null;
    }

    StringBuffer sb = new StringBuffer(ba.length * 2);
    String hexNumber;
    for (int x = 0; x < ba.length; x++) {
        hexNumber = "0" + Integer.toHexString(0xff & ba[x]);

        sb.append(hexNumber.substring(hexNumber.length() - 2));
    }
    return sb.toString();
} 

'programming > java' 카테고리의 다른 글

Java SE의 정규 표현식(정규식, Regular Expressions)  (0) 2009.09.15
필수 자바 라이브러리  (0) 2009.04.07
Singleton Templet  (0) 2008.09.23
XPath 간단예제들  (0) 2008.09.18
XOM 시작하기(Creating XML Documents)  (0) 2008.09.18

Singleton Templet

싱글톤 코딩시 참조.

public class ${Name} {
    private static ${Name} instance;

    public static ${Name} getInstance() {
        if (instance == null) {
            synchronized (${Name}.class) {
                // double-check idiom
                if (instance == null) {
                    instance = new ${Name}();
                }
            }
        }
        return instance;
    }

    private ${Name}() {
    }
}

'programming > java' 카테고리의 다른 글

필수 자바 라이브러리  (0) 2009.04.07
[JAVA] hex to byte[], byte[] to hex  (0) 2008.10.07
XPath 간단예제들  (0) 2008.09.18
XOM 시작하기(Creating XML Documents)  (0) 2008.09.18
XOM 소개  (0) 2008.09.18

XPath 간단예제들

	
		
			
				rock
				111
			
			
				stripe
				222
			
		
	

  

1. ddd의 TEXT가 rock인 것 가져오기

expresion= /aaa/bbb/ccc/ddd[text() = 'rock']

  

2. ddd의 TEXT가 rock인 것 형제들 가져 오기

expresion= /aaa/bbb/ccc/ddd[text() = 'rock']/following-sibiling::*

  

3. ddd의 attribute aaa가 bbb인 것 가져오기

expresion=/aaa/bbb/ccc/ddd[@aaa='bbb']

  

4. 2번째 ddd 가져오기

expresion=/aaa/bbb/ccc/ddd[2]

  

5. 마지막 ddd 가져 오기

expresion=/aaa/bbb/ccc/ddd[last()]

  

'programming > java' 카테고리의 다른 글

[JAVA] hex to byte[], byte[] to hex  (0) 2008.10.07
Singleton Templet  (0) 2008.09.23
XOM 시작하기(Creating XML Documents)  (0) 2008.09.18
XOM 소개  (0) 2008.09.18
SystemProp.java  (0) 2008.09.18