`
younglibin
  • 浏览: 1193151 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

long 转换为 byte后正确排序

 
阅读更多

  关于 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;
	}
}

 

分享到:
评论

相关推荐

    字节与数字类型转换小工具

    用来对大小端排序的字节数组进行解析的小工具,包括转为Int32,UInt32,float, double, Int16, UInt16, Long等

    XTCP:一个便捷的TCP消息包拼装和解析框架

    XTCP协议一个便捷的TCP消息包拼装和解析框架关于我特征简单通过@Protocol和@ProtocolField的配置,即可让实体对象拥有自动转换为TCP传输的字节数据和自动字节数据解析。支持byte,short,int,long,byte [],short ...

    基于HBase Thrift接口的一些使用问题及相关注意事项的详解

    1. 字节的存放顺序HBase中,由于row(row key和column family、column qualifier、time stamp)是按照字典序进行排序的,因此,对于short、int、long等类型的数据,通过Bytes.toBytes(…)转换成byte数组后,必须按照...

    VBSCRIPT中文手册

    CByte 函数 返回已被转换为字节子类型的变体的表达式。 CCur 函数 返回已被转换为货币子类型的变体的表达式。 CDate 函数 返回已被转换为日期子类型的变体的表达式。 CDbl 函数 返回已被转换为双精度子类型的变体...

    vb Script参考文档

    CByte 函数 返回已被转换为字节子类型的变体的表达式。 CCur 函数 返回已被转换为货币子类型的变体的表达式。 CDate 函数 返回已被转换为日期子类型的变体的表达式。 CDbl 函数 返回已被转换为双精度子类型的变体...

    VBScript 语言参考

    CByte 函数 返回已被转换为字节子类型的变体的表达式。 CCur 函数 返回已被转换为货币子类型的变体的表达式。 CDate 函数 返回已被转换为日期子类型的变体的表达式。 CDbl 函数 返回已被转换为双精度子类型的变体...

    VBScript 语言参考中文手册CHM

    CLng 函数 返回已被转换为 Long 子类型的变体的表达式。 颜色常数 颜色常数列表。 比较常数 用于比较运算的常数列表。 连接运算符 (&) 强制两个表达式的字符串连接。 Const 语句 声明用于字母值的常数。 Cos ...

    VBSCRIP5 -ASP用法详解

    CByte 函数 返回已被转换为字节子类型的变体的表达式。 CCur 函数 返回已被转换为货币子类型的变体的表达式。 CDate 函数 返回已被转换为日期子类型的变体的表达式。 CDbl 函数 返回已被转换为双精度子类型的变体...

    java_se_learning:java se learning project JAVA基础学习笔记和演示案例代码项目

    java se学习知识点整理 ...JAVA从小到大排列顺序:byte、short、char、int、folat、long、double、boolean JAVA中的引用数据类型有:类(class)、接口(interface)、数组([])。 JAVA中的类型转换:  JAVA中

    leetcode-java

    整型:byte(8位),short(16位),int(32位,例如:123),long(64位,例如:123L,123l) 浮点型:float(32位,例如:1.23f,1.23F),double(64位,例如:1.23) 字符型:char(16位) 布尔型:布尔型 在...

    一些C面试题,希望能对大家有帮助

    从以上分析可以看出,把局部变量改变为静态变量后是改变了它的存储方式即改变了它的生存期。把全局变量改变为静态变量后是改变了它的作用域,限制了它的使用范围。 static函数与普通函数作用域不同。仅在本文件。只...

    java经典面试2010集锦100题(不看你后悔)

    D) 程序将字符型转换为unicode编码并和b的数值相加的和输出。 题目3: 下面的说法中,错误的是:d(选择1项) A) 在Java中标志符可以是字母、下划线、数字或$符号。 B) 在Java中布尔类型不能和数字之间不能来回...

    net学习笔记及其他代码应用

    45.swtich是否能作用在byte上,是否能作用在long上,是否能作用在String上? 答:switch(expr1)中,expr1是一个整数表达式。因此传递给 switch 和 case 语句的参数应该是 int、 short、 char 或者 byte。long,...

    C 语言编程常见问题解答.chm

    6.7 怎样将数字转换为字符串? 6.8 怎样将字符串转换为数字? 6.9 怎样打印字符串的一部分? 6.10 怎样判判断两个字符串是否相同? 第7章 指针和内存分配 7.1 什么是间接引用(indirection)? 7.2 最多...

    〖程序设计基础〗练习题3及答案

    5.在Java语言中,将后缀名为_____的源代码编译后形成后缀名为______的字节码文件。 6.设有整型数组的定义:int a[]=new int[8]; ,则a.length的值为_____。 7.栈是一种先进____________的线性数据结构,而队列是先进...

    java 面试题 总结

    在实现中,assertion就是在程序中的一条语句,它对一个boolean表达式进行检查,一个正确程序必须保证这个boolean表达式的值为true;如果该值为false,说明程序已经处于不正确的状态下,系统将给出警告或退出。...

    freemarker总结

    2,使用+运算符时,如果一边是数字,一边是字符串,就会自动将数字转换为字符串再连接,如:${3 + "5"},结果是:35 使用内建的int函数可对数值取整,如: ${ (x/2)?int } ${ 1.1?int } ${ 1.999?int } ${ -1.1?int } ...

    JAVA面试题最全集

    如何将数值型字符转换为数字(Integer,Double) 如何将数字转换为字符 如何取小数点前两位,并四舍五入。 4.日期和时间 如何取得年月日,小时分秒 如何取得从1970年到现在的毫秒数 如何获取某个日期是当月的...

    超级有影响力霸气的Java面试题大全文档

    在实现中,assertion就是在程序中的一条语句,它对一个boolean表达式进行检查,一个正确程序必须保证这个boolean表达式的值为true;如果该值为false,说明程序已经处于不正确的状态下,系统将给出警告或退出。...

    java面试题

    jsp被编译之后会转换为servlet执行 java基本数据类型有哪些?String是不是基本数据类型,他们有何区别? 答:基本数据类型8种:int、short、byte、long、float、double、char、boolean String不是基本数据类型,引用...

Global site tag (gtag.js) - Google Analytics