You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Mike Streeton <mi...@ardentia.co.uk> on 2006/01/26 13:28:38 UTC
Range number queries
For the recent questions about this here are a couple of methods for
encoding/decoding long values that will be sorted into order by a range
query
public static String encodeLong(long num) {
String hex = Long.toHexString(num < 0 ? Long.MAX_VALUE -
(0xffffffffffffffffL ^ num) : num);
hex = (num < 0 ? "N" : "P")+"0000000000000000".substring(0,
16-hex.length()) + hex;
return hex;
}
public static long decodeLong(String hex) {
long num = Long.parseLong(hex.substring(1,17), 16);
return hex.charAt(0) == 'N' ? (Long.MAX_VALUE - num) ^
0xffffffffffffffffL : num;
}
Hope this helps
Mike
www.ardentia.com the home of NetSearch