关于 java中long类型数据转换为byte[]数组排序,不能正确排序问题,目前的解决方案为:
long 转为 byte[] 时,对 byte[] 中的每一个byte 做一次 减去 128 的操作:如long2Orderbyte 中的实现方式;
byte[]转为 long 时 ,需要对byte[] 中的每一位 做一次 加 128 的操作: 如orderByte2long 中的实现方式;
经过测试,数据经过2次往返转换,数据值不变,且byte[]可以正确的排序。
public static byte[] long2Orderbyte(long n)
{
byte[] b = new byte[8];
b[0] = (byte) ((byte) (n >> 56) - 128);
b[1] = (byte) ((byte) (n >> 48) - 128);
b[2] = (byte) ((byte) (n >> 40) - 128);
b[3] = (byte) ((byte) (n >> 32) - 128);
b[4] = (byte) ((byte) (n >> 24) - 128);
b[5] = (byte) ((byte) (n >> 16) - 128);
b[6] = (byte) ((byte) (n >> 8) - 128);
b[7] = (byte) ((byte) (n >> 0) - 128);
return b;
}
public static long orderByte2long(byte[] b, int offset) throws IllegalArgumentException
{
if (b == null || offset < 0 || b.length < offset + 8) {
log.warn("target or startIndex error");
throw new IllegalArgumentException("target or startIndex error");
}
return ((((long) (b[offset] + 128) & 0xff) << 56) | (((long) (b[offset + 1] + 128) & 0xff) << 48) | (((long) (b[offset + 2] + 128) & 0xff) << 40)
| (((long) (b[offset + 3] + 128) & 0xff) << 32) | (((long) (b[offset + 4] + 128) & 0xff) << 24) | (((long) (b[offset + 5] + 128) & 0xff) << 16)
| (((long) (b[offset + 6] + 128) & 0xff) << 8) | (((long) (b[offset + 7] + 128) & 0xff) << 0));
}
package com.younglibin; public final class TypeConversionUtil { private TypeConversionUtil() { } public static int byte2int(byte[] b, int offset) throws IllegalArgumentException { if (b == null || offset < 0 || b.length < offset + 4) { throw new IllegalArgumentException("target or startIndex error"); } return b[offset + 3] & 0xff | (b[offset + 2] & 0xff) << 8 | (b[offset + 1] & 0xff) << 16 | (b[offset] & 0xff) << 24; } public static void int2byte(int n, byte[] b, int offset) throws IllegalArgumentException { if (b == null || offset < 0 || b.length < offset + 4) { throw new IllegalArgumentException("target or startIndex error"); } b[offset] = (byte) (n >> 24); b[offset + 1] = (byte) (n >> 16); b[offset + 2] = (byte) (n >> 8); b[offset + 3] = (byte) n; } public static short byte2short(byte[] b, int offset) throws IllegalArgumentException { if (b == null || offset < 0 || b.length < offset + 2) { throw new IllegalArgumentException("target or startIndex error"); } return (short) (((b[offset] < 0 ? b[offset] + 256 : b[offset]) << 8) + (b[offset + 1] < 0 ? b[offset + 1] + 256 : b[offset + 1])); } public static void short2byte(short n, byte[] b, int offset) throws IllegalArgumentException { if (b == null || offset < 0 || b.length < offset + 2) { throw new IllegalArgumentException("target or startIndex error"); } b[offset] = (byte) ((n & 0xFF00) >> 8); b[offset + 1] = (byte) (n & 0xFF); } public static long byte2long(byte[] b, int offset) throws IllegalArgumentException { if (b == null || offset < 0 || b.length < offset + 8) { throw new IllegalArgumentException("target or startIndex error"); } return ((((long) b[offset] & 0xff) << 56) | (((long) b[offset + 1] & 0xff) << 48) | (((long) b[offset + 2] & 0xff) << 40) | (((long) b[offset + 3] & 0xff) << 32) | (((long) b[offset + 4] & 0xff) << 24) | (((long) b[offset + 5] & 0xff) << 16) | (((long) b[offset + 6] & 0xff) << 8) | (((long) b[offset + 7] & 0xff) << 0)); } public static void long2byte(long n, byte[] b, int offset) throws IllegalArgumentException { if (b == null || offset < 0 || b.length < offset + 8) { throw new IllegalArgumentException("target or startIndex error"); } b[offset + 0] = (byte) (n >> 56); b[offset + 1] = (byte) (n >> 48); b[offset + 2] = (byte) (n >> 40); b[offset + 3] = (byte) (n >> 32); b[offset + 4] = (byte) (n >> 24); b[offset + 5] = (byte) (n >> 16); b[offset + 6] = (byte) (n >> 8); b[offset + 7] = (byte) (n >> 0); } public static byte[] long2byte(long n) { byte[] b = new byte[8]; b[0] = (byte) (n >> 56); b[1] = (byte) (n >> 48); b[2] = (byte) (n >> 40); b[3] = (byte) (n >> 32); b[4] = (byte) (n >> 24); b[5] = (byte) (n >> 16); b[6] = (byte) (n >> 8); b[7] = (byte) (n >> 0); return b; } public static byte[] long2Orderbyte(long n) { byte[] b = new byte[8]; b[0] = (byte) ((byte) (n >> 56) - 128); b[1] = (byte) ((byte) (n >> 48) - 128); b[2] = (byte) ((byte) (n >> 40) - 128); b[3] = (byte) ((byte) (n >> 32) - 128); b[4] = (byte) ((byte) (n >> 24) - 128); b[5] = (byte) ((byte) (n >> 16) - 128); b[6] = (byte) ((byte) (n >> 8) - 128); b[7] = (byte) ((byte) (n >> 0) - 128); return b; } public static long orderByte2long(byte[] b, int offset) throws IllegalArgumentException { return ((((long) (b[offset] + 128) & 0xff) << 56) | (((long) (b[offset + 1] + 128) & 0xff) << 48) | (((long) (b[offset + 2] + 128) & 0xff) << 40) | (((long) (b[offset + 3] + 128) & 0xff) << 32) | (((long) (b[offset + 4] + 128) & 0xff) << 24) | (((long) (b[offset + 5] + 128) & 0xff) << 16) | (((long) (b[offset + 6] + 128) & 0xff) << 8) | (((long) (b[offset + 7] + 128) & 0xff) << 0)); } public static void main(String args[]) { int a = 99200, b = 99199; System.out.println("a>b:" + (a > b)); byte[] abyte = long2byte(a), bbyte = long2byte(b); System.out.println("abyte > abyte :" + compare(abyte, bbyte)); byte[] aNewbyte = long2Orderbyte(a), bNewbyte = long2Orderbyte(b); System.out.println("aNewbyte > aNewbyte : " + compare(aNewbyte, bNewbyte)); System.out.println("a = " + orderByte2long(aNewbyte, 0)); System.out.println("b = " + orderByte2long(bNewbyte, 0)); testOrder(); } public static void testOrder() { long b = 0; for (long a = 1; a < 100000; a++) { b = a - 1; byte[] aoldbyte = long2byte(a), boldbyte = long2byte(b); if (compare(aoldbyte, boldbyte) > 0) { } else if (compare(aoldbyte, boldbyte) < 0) { System.out.println("error" + a); } else { System.out.println("equals"); } } System.out.println("use order --------------------------"); for (long a = 1; a < 100000; a++) { b = a - 1; byte[] aoldbyte = long2Orderbyte(a), boldbyte = long2Orderbyte(b); if (compare(aoldbyte, boldbyte) > 0) { } else if (compare(aoldbyte, boldbyte) < 0) { System.out.println("error" + a); } else { System.out.println("equals"); } if (a != orderByte2long(aoldbyte, 0)) { System.out.println("use orderByte2long error:" + a); } if (b != orderByte2long(boldbyte, 0)) { System.out.println("use orderByte2long error:" + b); } } } public static int compare(byte[] a, byte[] b) { int index = 0; int reFlag = 0; while (index < 8) { if (a[index] > b[index]) { reFlag = 1; break; } else if (a[index] < b[index]) { reFlag = -1; break; } index++; } return reFlag; } }