博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
你真正理解java的字符类型了吗?(bit,byte,short,int等字符类型)
阅读量:4288 次
发布时间:2019-05-27

本文共 2959 字,大约阅读时间需要 9 分钟。

下面你会真正理解java的bit,byte,short,int等字符类型
package sort.bing.com;import java.io.ByteArrayOutputStream;import java.io.DataOutputStream;import java.io.UnsupportedEncodingException;public class ByteUtils {	public static byte[] int2byte(int res) {		byte[] targets = new byte[4];		targets[0] = (byte) (res & 0xff);// 最低位		targets[1] = (byte) ((res >> 8) & 0xff);// 次低位		targets[2] = (byte) ((res >> 16) & 0xff);// 次高位		targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。		return targets;	}	public static int byte2int(byte[] res) {		// 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000		int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) // | 表示安位或				| ((res[2] << 24) >>> 8) | (res[3] << 24);		return targets;	}	public static byte[] intToByteArray(int i) throws Exception {		ByteArrayOutputStream buf = new ByteArrayOutputStream();		DataOutputStream out = new DataOutputStream(buf);		System.out.println("i:" + i);		out.writeInt(i);		byte[] b = buf.toByteArray();		System.out.println("i:" + b);		out.close();		buf.close();		return b;	}	/**	 * 将16位的short转换成byte数组	 * 	 * @param s	 *            short	 * @return byte[] 长度为2	 * */	public static byte[] shortToByteArray(short s) {		byte[] targets = new byte[2];		for (int i = 0; i < 2; i++) {			int offset = (targets.length - 1 - i) * 8;			targets[i] = (byte) ((s >>> offset) & 0xff);		}		return targets;	}	/**	 * 注释:字节数组到short的转换!	 * 	 * @param b	 * @return	 */	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[]转换成16进制进制字符串	 * @param b	 * @return	 */	public static String bytes2HexString(byte[] b) {  		  String ret = "";  		  for (int i = 0; i < b.length; i++) {  		   String hex = Integer.toHexString(b[ i ] & 0xFF);  		   if (hex.length() == 1) {  		    hex = '0' + hex;  		   }		   ret += hex.toUpperCase();  		  }  		  return ret;  		}		/**	 * byte[]转换成bit	 * @param b	 * @return	 */	public static String bytesToBits(byte[] bytes) {		StringBuffer sb = new StringBuffer();		for(byte b:bytes){			sb.append(byteToBits(b));		}		return sb.toString();	}		/**	 * byte转换成8位bit	 * @param b	 * @return	 */	public static String byteToBits(byte b) {		int z = b; z |= 256;		String str = Integer.toBinaryString(z);		int len = str.length();		return str.substring(len-8, len);	}		/**	 * 计算校验和	 * @param bytes	 * @return	 */	public static final int calculateCheckSum(byte[] bytes) {  	   int sum = 0;	   for (byte b : bytes) {	      sum += (short)b;  	   }	   return sum>65535 ? (sum-65535) : sum;	}		public static void main(String[] args) throws UnsupportedEncodingException {//		String s = "12" ;//		short n = 100;//		byte[] buf = s.getBytes("UTF-8");//		byte[] buf2 = ByteUtils.shortToByteArray(n);//		System.out.println(calculateCheckSum(buf));//		System.out.println(calculateCheckSum(buf2));//		System.out.println(bytes2HexString(buf2));		byte i = -112;		System.out.println(i & 0xff);	}}

转载地址:http://ultgi.baihongyu.com/

你可能感兴趣的文章
iOS之支付宝集成(二)
查看>>
java基础(一)Java学习路线
查看>>
iOS之苹果自带的json解析NSJSONSerialization(序列化)
查看>>
iOS中坐标转换
查看>>
java 基础二
查看>>
java基础(三)方法/数组/堆栈/
查看>>
java基础(四)二维数组/
查看>>
java基础(五)面向对象/类/对象/形式参数/局部和成员变量
查看>>
java基础(六)关键字/private/this/static/构造方法/
查看>>
java基础(七)/面向对像
查看>>
java基础(八)Math/代码块/继承成员方法指南的关系/继承中成员变量之间的关系/方法的重写/继承中构造方法之间的关系/this和super的区别
查看>>
iOS之AFNetWorking基本用法(一)上传、下载
查看>>
java基础(九)关键字final/多态/抽象类/关键字abstract/接口
查看>>
java中的错误集合
查看>>
java基础(十)形式参数和返回值/链式编程/包/权限修饰符/内部类
查看>>
java集成开发环境eclipse/MyEclipse
查看>>
C语言char *p 和 cha'r p[10]的区别/sizeof和strlen的区别
查看>>
iOS发布新应用/更新新版本的流程
查看>>
java的API/Object
查看>>
java基础/Scanner类/String类
查看>>