본문 바로가기

Java/Java 기초

Base62 encoding decoding

public class Base62 {
	final int RADIX = 62;
	final String CODEC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	
	public String encoding(long param) {
		StringBuffer sb = new StringBuffer();
		while(param > 0) {
			sb.append(CODEC.charAt((int) (param % RADIX)));
			param /= RADIX;
		}
		return sb.toString();
	}
	
	public long decoding(String param) {
		long sum = 0;
		long power = 1;
		for (int i = 0; i < param.length(); i++) {
			sum += CODEC.indexOf(param.charAt(i)) * power;
			power *= RADIX;
		}
		return sum;
	}
}

'Java > Java 기초' 카테고리의 다른 글

Properties Class 사용  (0) 2018.06.12
SHA256 encoding  (0) 2018.05.31
Java Queue 구현  (0) 2018.05.24
Java Stack 구현  (0) 2018.05.24
XSS(Cross-site scripting) 특수문자 치환  (0) 2018.05.10