无论是从Integer还是Long还是Byte的类里都找不到把字节数组转换为数字的方法,无奈自己写,吭哧吭哧写完了之后发现原来BigInteger里边有从字节数组转换的构造函数,网上致我们吭哧吭哧找也没找到,别人也都是自己写的转换函数,真是费神不讨好啊。

看了一下BigInteger的源代码,叹为观止。不过我只用得到转换成字节数组和从字节数组构造这两个方法。

package com.idealisan.exchange.bytes;

import java.math.BigInteger;

public class Bytes {

	/**
	 * Big endian converter. bytes[0] will be placed at the highest byte of the long
	 * type value. If you want a little endian result, use
	 * <code>Long.reverseBytes(long i)</code> to do that easily.
	 * 
	 * @param bytes
	 * @return a long type number converted from 8 bytes of that array using big
	 *         endian strategy.
	 */
	public static long toLong(byte[] bytes, int offset) {
		long ret = 0;

		for (int i = 0; i < 8; i++) {
			ret = ret << 8;
			ret = ret | bytes[offset + i];
		}

		return ret;
	}
	
	
	/**
	 * Big endian converter. bytes[0] will be placed at the highest byte of the integer
	 * type value. If you want a little endian result, use
	 * <code>Integer.reverseBytes(int i)</code> to do that easily.
	 * 
	 * @param bytes
	 * @return a integer type number converted from 8 bytes of that array using big
	 *         endian strategy.
	 */
	public static int toInteger(byte[] bytes, int offset) {
		long ret = 0;
		byte[] temp=new byte[8];
		System.arraycopy(bytes, offset,temp, 4, 4);
		return (int) toLong(temp, 0);
	}
	
	
	public static void main(String[] args) {
		byte[] bytes = new byte[] { 0x01, 0x01,  0x01,  0x01 };
		
		System.out.println(toInteger(bytes, 0));
		
		BigInteger bigInteger = new BigInteger(bytes);
		System.out.println(bigInteger);
	}
}

最近发现了比 ByteArrayOutputStream 更加合理、适用于更多原始数据类型的合并数组的方法。
Java的NIO包中包含了许多缓冲(Buffer)类,使用这些类可以处理所有原始数据类型的数组,用起来和ByteArrayOutputStream一样简单方便。

import java.nio.*;

public class ConcatArrays {


    public static void main(String[] args) {
        int[] myNumbers=new int[]{1,2,3,4};
        int[] yourNumbers=new int[]{5,6,7};
        int[] theirNumbers=new int[]{8,9,0};

        //按需要分配buffer        
        IntBuffer intBuffer = IntBuffer.allocate(myNumbers.length+yourNumbers.length+theirNumbers.length);

        //放入到buffer        
        intBuffer.put(myNumbers);
        intBuffer.put(yourNumbers);
        intBuffer.put(theirNumbers);

        //得到合并后的数组        
        int[] allNumber = intBuffer.array();

        //显示合并后的数组        
        for (int i :allNumber ) {
            System.out.print(i);
            System.out.print(", ");
        }

        //各种数据原始类型都有buffer可用,非常方便        
        LongBuffer longBuffer = LongBuffer.allocate(10);
        DoubleBuffer doubleBuffer = DoubleBuffer.allocate(10);
        FloatBuffer floatBuffer = FloatBuffer.allocate(10);
        ByteBuffer byteBuffer = ByteBuffer.allocate(10);
        ShortBuffer shortBuffer = ShortBuffer.allocate(10);
        CharBuffer charBuffer = CharBuffer.allocate(10);
    }
}