`
san_yun
  • 浏览: 2594466 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

各种进制基础知识

阅读更多

10进制是人类最熟悉的数字计算

2进制是机器最基本的单位

16进制呢?是为了方便表示最基本的数据传输单位字节,因为一个字节8位,16进制2位就能轻松表示1个字节。

比如 15

16进制:print 0x0F

2进制:print 0b1111

255

 

16进制:print 0xFF

2进制:print 0b11111111


如何把一个数字转换成字节数组?

以10001为例:

i = 10001
bin(i):
'0b10011100010001'
int两个字节表示:

  • 高位:100111
  • 低位:00010001

获取高位的值:big = i>>8      //位移8位
获取低位的值:little = (byte)i17   //强制转换成byte会丢弃超过byte范围的高位值

decode:
result = big<<8 | little
print result

 

这里解释一下:

要了解移位运算,需明白二进制计数

以单字节为例,比如0x55=0b01010101

如果将该数值左移1位,就变成了 0b10101010(最右边这个0是移空了以后补的),这个数值是0xaa

从数学上看,左移1位等于乘以2,右移1位等于除以2,然后再取整,移位溢出的丢弃。

 

全部代码:

 

public class BruteForceCoding {

	private final static int BSIZE = Byte.SIZE / Byte.SIZE; // 1
	private final static int SSIZE = Short.SIZE / Byte.SIZE; // 2
	private final static int ISIZE = Integer.SIZE / Byte.SIZE; // 4
	private final static int LSIZE = Long.SIZE / Byte.SIZE; // 8

	private final static int BYTEMASK = 0xFF; // 8 bits

	public static String byteArrayToDecimalString(byte[] bArray) {
		StringBuilder rtn = new StringBuilder();
		for (byte b : bArray) {
			rtn.append(b & BYTEMASK).append(" ");
		}
		return rtn.toString();
	}

	// Warning: Untested preconditions (e.g., 0 <= size <= 8)
	public static int encodeIntBigEndian(byte[] dst, long val, int offset, int size) {
		for (int i = 0; i < size; i++) {
			dst[offset++] = (byte) (val >> ((size - i - 1) * Byte.SIZE));
		}
		return offset;
	}

	// Warning: Untested preconditions (e.g., 0 <= size <= 8)
	public static long decodeIntBigEndian(byte[] val, int offset, int size) {
		long rtn = 0;
		for (int i = 0; i < size; i++) {
			rtn = (rtn << Byte.SIZE) | ((long) val[offset + i] & BYTEMASK);
		}
		return rtn;
	}

	public static void main(String[] args) {
//		System.out.println(Long.toBinaryString(10001));
//		System.out.println(Long.toBinaryString(17));
//		System.out.println((byte) 10001);
		
		byte byteVal = 101; // one hundred and one
		short shortVal = 10001; // ten thousand and one
		int intVal = 100000001; // one hundred million and one
		long longVal = 1000000000001L; // one trillion and one

		byte[] message = new byte[BSIZE + SSIZE + ISIZE + LSIZE];
		// Encode the fields in the target byte array
		int offset = encodeIntBigEndian(message, byteVal, 0, BSIZE);
		offset = encodeIntBigEndian(message, shortVal, offset, SSIZE);
		offset = encodeIntBigEndian(message, intVal, offset, ISIZE);
		encodeIntBigEndian(message, longVal, offset, LSIZE);
		System.out.println("Encoded message: " + byteArrayToDecimalString(message));

		// Decode several fields
		long value = decodeIntBigEndian(message, BSIZE, SSIZE);
		System.out.println("Decoded short = " + value);
		value = decodeIntBigEndian(message, BSIZE + SSIZE + ISIZE, LSIZE);
		System.out.println("Decoded long = " + value);

		// Demonstrate dangers of conversion
		offset = 4;
		value = decodeIntBigEndian(message, offset, BSIZE);
		System.out.println("Decoded value (offset " + offset + ", size " + BSIZE + ") = " + value);
		byte bVal = (byte) decodeIntBigEndian(message, offset, BSIZE);
		System.out.println("Same value as byte = " + bVal);
		
		//10001
	}

}

 简单解释一下:
byteArrayToDecimalString():
      该方法把给定数组中的每个字节作为一个无符号十进制数打印出来。BYTEMASK 的作用是防止在字节数值转换成 int 类型时,发生符号扩展(sign-extended),即转换成无符号整型。

encodeIntBigEndian():

     赋值语句的右边,首先将数值向右移动,以使我们需要的字节处于该数值的低 8 位中。然后,将移位后的数转换成 byte 型,并存入字节数组的适当位置。在转换过程中,除了低 8位以外,其他位都将丢弃。这个过程将根据给定数值所占字节数迭代进行。该方法还将返回存入数值后字节数组中新的偏移位置,因此我们不必做额外的工作来跟踪偏移量。

decodeIntBigEndian():
     根据给定数组的字节大小进行迭代,通过每次迭代的左移操作,将所取得字节的值累积到一个 long 型整数中。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics