byte构造数据总结

1  java基本类型说明
    1、byte            8位
    2、short          16位
    3、int              32位
    4、double       64位
    在java中一个byte称为一个字节,所以一个short需要用2个byte来构造,一个int需要4个byte来构造,一个double需要8个byte来构造,一个中文类型的字符占2个byte。

2  byte类型学习笔记
    1、用byte[]构造字符串的时候byte by[] = {0}表示空格
         用byte by[] = {0},构造字符串时表示一个空格,byte by[] = {0,0}表示两个空格......
         byte by[] = {0};
         String str = "fsd" + new String(by)+"sfasfsda";
         str = "fsd sfasfsda";

3  代码演示

package com;

public class ByteUtil {
	/**
	 * 用byte构造int数据,低位
	 * 一个byte(字节)占8位,一个int数据占32位,一个int类型的数据占4个字节,需要4个byte数据类接收
	 */
	public static int byteToInt(byte[] b) {
		int s = 0;
		int s0 = b[0] & 0xff;		
		int s1 = b[1] & 0xff;
		int s2 = b[2] & 0xff;
		int s3 = b[3] & 0xff;

		s3 <<= 24;
		s2 <<= 16;
		s1 <<= 8;
		s = s0 | s1 | s2 | s3;
		return s;
	}

	/**
	 * 用byte[]构造int数据,高位
	 */
	public static int byteToInt2(byte[] b) {
		int s = 0;
		int s0 = b[0] & 0xff;
		int s1 = b[1] & 0xff;
		int s2 = b[2] & 0xff;
		int s3 = b[3] & 0xff;

		s0 <<= 24;
		s1 <<= 16;
		s2 <<= 8;
		s = s0 | s1 | s2 | s3;
		return s;
	}
	

	/**
	 * 将in转换为byte[]
	 */
	public static byte[] intToByte(int i) {
		byte[] result = new byte[4];
		result[0] = (byte) ((i >> 24) & 0xFF);
		result[1] = (byte) ((i >> 16) & 0xFF);
		result[2] = (byte) ((i >> 8) & 0xFF);
		result[3] = (byte) (i & 0xFF);
		return result;
	}
	
	/**
	 * 将short转换为byte[]
	 * @param number
	 * @return
	 */
	public static byte[] shortToByte(short number) {
		int temp = number;
		byte[] b = new byte[2];
		for (int i = 0; i < b.length; i++) {
			b[i] = new Integer(temp & 0xff).byteValue();
			temp = temp >> 8;
		}
		return b;
	}
	

	/**
	 * 用byte[]构造short数据 低位
	 */
	public static short byteToShort(byte[] b) {
		short s = 0;
		short s0 = (short) (b[0] & 0xff);// 最低位
		short s1 = (short) (b[1] & 0xff);
		s1 <<= 8;
		s = (short) (s0 | s1);
		return s;
	}

	/**
	 * 用byte[]构造short数据,高位
	 */
	public static short byteToShort2(byte[] b) {
		short s = 0;
		short s0 = (short) (b[0] & 0xff);// 最低位
		short s1 = (short) (b[1] & 0xff);
		s0 <<= 8;
		s = (short) (s0 | s1);
		return s;
	}
	
	/**
	 * byte[]构造Long
	 */
	public static long byteToLong(byte[] b) {
		long num = 0;
		for (int ix = 0; ix < 8; ++ix) {
			num <<= 8;
			num |= (b[ix] & 0xff);
		}
		return num;
	}

	/**
	 * 截取byte[]数据,不够就补组成byte[]数组
	 * @param src
	 * @param begin 开始位置
	 * @param count 取少位
	 * @return
	 */
	public static byte[] subBytes(byte[] src, int begin, int count) {
		byte[] bs = new byte[count];
		for (int i = begin; i < begin + count; i++)
			bs[i - begin] = src[i];
		return bs;
	}

	/**
	 * 填充数据,不够位就补
	 * @param bytes 字符串
	 * @param length 填充长度
	 */
	public static byte[] makeUpBytes(byte[] bytes, int length) {
		int oLen;
		if (bytes == null) {
			oLen = 0;
		}
		oLen = bytes.length;
		if (oLen == length) {
			return bytes;
		}
		byte[] nBytes = new byte[length];
		for (int i = 0; i < length; i++) {
			if (i < oLen) {
				nBytes[i] = bytes[i];
			} else {
				nBytes[i] = 0;
			}
		}
		return nBytes;
	}
}

4  测试代码演示

package com;

public class ByteUtilTest {
	public static void main(String[] args) {
		byteStructureStr();
		strByte();
		getByteStr();
		
		byte by[] = intToByte();
		int num = byteToInt(by);
		System.out.println(num);
	}
	
	/**
	 * 将String转换为byte[],然后将byte[]元素拼成字符串输出
	 */
	public static void byteStructureStr(){
		//将String转换成byte[]
		String str = "你妹。";
		byte byte1[] = str.getBytes();
		//将str转换后的byte字节数组已字符串的形式打印出来的字符串
		String byteStr = "";		
		for(int i = 0; i < byte1.length; i++){
			String b = byte1[i] + "";
			if(i < byte1.length -1){
				byteStr += b + ",";
			}else{
				byteStr += b;
			}
		}
		System.out.println(byteStr);
	}
	
	/**
	 * byte[]构造String
	 */
	public static void strByte(){
		//byte构造String
		byte byte2[] = {-28, -67, -96, -27, -90, -71, -29, -128, -126};
		String nimei = new String(byte2);
		System.out.println(nimei);
	}
	
	/**
	 * 截取byte数组,不够就补位,然后用数构造字符串
	 */
	public static void getByteStr(){
		byte by[] = {-45,-78,-28, -67, -96, -27, -90, -71, -29, -128, -126, -37};
		int begin = 2;
		int length = 9;
		//截取后的byte数组,将这里的“9”修改为大于9的时候就是长度不够补位
		byte by2[] = new byte[length];
		int bynum = 0;
		for (int i = begin; i < begin + length; i++) {
			by2[bynum] = by[begin+bynum];
			bynum++;
		}
		
		System.out.println(new String(by2));
	}
	
	
	/**
	 * 将in转换为byte[],低位
	 */
	public static byte[] intToByte() {
		int i = 10000;
		byte[] result = new byte[4];
		result[0] = (byte) (i & 0xFF);
		result[1] = (byte) ((i >> 8) & 0xFF);
		result[2] = (byte) ((i >> 16) & 0xFF);
		result[3] = (byte) ((i >> 24) & 0xFF);
		return result;
	}
	
	/**
	 * 将byte[]转化为int,低位
	 * @param b
	 * @return
	 */
	public static int byteToInt(byte[] b) {
		int s = 0;
		int s0 = b[0] & 0xff;		
		int s1 = b[1] & 0xff;
		int s2 = b[2] & 0xff;
		int s3 = b[3] & 0xff;

		s3 <<= 24;
		s2 <<= 16;
		s1 <<= 8;
		s = s0 | s1 | s2 | s3;
		return s;
	}
	
	/**
	 * 将int转换为byte[],高位
	 */
	public static byte[] intToByte2() {
		int i = 10000;
		byte[] result = new byte[4];
		result[0] = (byte) ((i >> 24) & 0xFF);
		result[1] = (byte) ((i >> 16) & 0xFF);
		result[2] = (byte) ((i >> 8) & 0xFF);
		result[3] = (byte) (i & 0xFF);
		return result;
	}
	
	/**
	 * 用byte[]构造int数据,高位
	 */
	public static int byteToInt2(byte[] b) {
		int s = 0;
		int s0 = b[0] & 0xff;
		int s1 = b[1] & 0xff;
		int s2 = b[2] & 0xff;
		int s3 = b[3] & 0xff;

		s0 <<= 24;
		s1 <<= 16;
		s2 <<= 8;
		s = s0 | s1 | s2 | s3;
		return s;
	}
}

猜你喜欢

转载自x125858805.iteye.com/blog/2156559