你真正理解java的字符类型了吗?(bit,byte,short,int等字符类型)
2014-03-09 11:53
423 查看
下面你会真正理解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); } }
相关文章推荐
- java中数据类型(short 和int)转换问题的理解
- java中byte short int的理解
- Java中的byte,short,char进行计算时都会提升为int类型。
- Java八种基本类型(byte、short、int、long、浮点数、char、boolean、基本类型转换)
- 08.java语言基础-整数类型(byte-short-int-long)
- java中byte、short、int、long、float、double类型的取值范围
- java byte、short、int、long、float、double、char基本数据类型范围
- Java 对byte,short,char,int,long 运算时自动类型转化情况说明
- java 彻底理解 byte char short int float long double
- C的无符号数据类型int,short,byte 转为 Java对应的数据long,char,short
- java 基本数值类型:byte short int long float double char
- java Byte和各数据类型(short,int,long,float,double)之间的转换
- java 彻底理解 byte char short int float long double
- java对byte,short,char,int,long运算时自动类型转化情况说明
- bit/byte/英文字符/汉字之间的换算及java八大基本数据类型的占字节数
- C的无符号数据类型int,short,byte 转为 Java对应的数据long,char,short
- Android学习心得(19) --- short、byte、Hex、int、ULeb128类型转化Java源码
- java 彻底理解 byte char short int float long double
- C的无符号数据类型int,short,byte 转为 Java对应的数据long,char,short
- java 彻底理解 byte char short int float long double (转)