Android

个人Android学习总结


java long类型、short类型与byte类型互转

<h2>java long类型、short类型与byte类型互转</h2> <pre><code class="language-java">package com.spt.chinamobile.utils; public class ByteUtil { /** * byte 转无符号 int * @param a * @return */ public int byte2UnsignedInt(byte a){ return a &amp; 0xff; } //20000000001 public static long byte2long(byte[] b){ long result =0 ; long temp = 0 ; int len = b.length ; for (int i = 0; i &lt; len; i++) { temp = (long)(b[i]&amp;0xff)&lt;&lt;(len-1-i)*8 ; result |=temp ; } return result ; } public static byte[] long2byte(long param,int len){ byte[] b = new byte[len] ; for (int i = 0; i &lt; len; i++) { b[i] = (byte) ((param &gt;&gt; 8 * (len - i - 1)) &amp; 0xff); // LogUtil.d("ByteUtil",""+b[i]+","); } return b; } /** * @功能 短整型与字节的转换,低8位在前,高8位在后 * @param 短整型 * @return 两位的字节数组 */ public static byte[] short2byte(short number) { int temp = number; byte[] b = new byte[2]; for (int i = 0; i &lt; b.length; i++) { b[i] = new Integer(temp &amp; 0xff).byteValue();// 得到低8位的值 temp = temp &gt;&gt; 8; // 向右移8位,得到高8位的值 } return b; } //number&gt;&gt;8&amp;0xff 得到高8位的值 //number&amp;0xff 得到低8位的值 /** * @功能 字节的转换与短整型 * @param 两位的字节数组 * @return 短整型 */ public static short byte2short(byte[] b) { short s = 0; short s0 = (short) (b[0] &amp; 0xff);// 最低位 short s1 = (short) (b[1] &amp; 0xff); s1 &lt;&lt;= 8; s = (short) (s0 | s1); return s; } public static int byteArrayToInt(byte[] valueBuf, int offset) { ByteBuffer converter = ByteBuffer.wrap(valueBuf); converter.order(ByteOrder.nativeOrder()); return converter.getInt(offset); } public static byte[] intToByteArray(int value) { ByteBuffer converter = ByteBuffer.allocate(4); converter.order(ByteOrder.nativeOrder()); converter.putInt(value); return converter.array(); } public static short byteArrayToShort(byte[] valueBuf, int offset) { ByteBuffer converter = ByteBuffer.wrap(valueBuf); converter.order(ByteOrder.nativeOrder()); return converter.getShort(offset); } public static byte[] shortToByteArray(short value) { ByteBuffer converter = ByteBuffer.allocate(2); converter.order(ByteOrder.nativeOrder()); short sValue = (short) value; converter.putShort(sValue); return converter.array(); } /** * Unicode码 转字符串 * @param unicode * @return */ public static String unicodeToCn(String unicode) { /** 以 \ u 分割,因为java注释也能识别unicode,因此中间加了一个空格*/ String[] strs = unicode.split("\\\\u"); String returnStr = ""; // 由于unicode字符串以 \ u 开头,因此分割出的第一个字符是""。 for (int i = 1; i &lt; strs.length; i++) { //returnStr += "\\u" + Integer.toString(chars[i], 16); returnStr += "\\u" + Integer.toHexString(chars[i]); } return returnStr; } /** * 字符创转Unicode码 * @param cn * @return */ public static String cnToUnicode(String cn) { char[] chars = cn.toCharArray(); System.out.println("chars.length = " + chars.length); String returnStr = ""; for (int i = 0; i &lt; chars.length; i++) { //returnStr += "\\u" + Integer.toString(chars[i], 16); returnStr += "\\u" + Integer.toHexString(chars[i]); } return returnStr; } } /** * 将字符串转化为 unicode码字节 * @param cn * @return */ public static byte[] cnToByteArray(String cn){ String unicodeString = cnToUnicode(cn); System.out.println("unicodeString = " + unicodeString); String[] hexs = unicodeString.split("\\\\u"); for (int i = 0; i &lt; hexs.length; i++) { System.out.println("hexs["+i+"] = " + hexs[i]+" "); } ByteBuffer byteBuffer = ByteBuffer.allocate((hexs.length-1)*2); byteBuffer.order(ByteOrder.BIG_ENDIAN); for (int i = 1; i &lt; hexs.length; i++) { int value = Integer.valueOf(hexs[i],16).intValue(); // byteBuffer.putShort((short)value); byteBuffer.put((byte) (value&gt;&gt;8));//高8位 byteBuffer.put((byte) (value &amp; 0xFF));//低8位 } String revertCn = unicodeToCn(unicodeString); System.out.println("revertCn = " + revertCn); String returnUnicode = ""; int capacity = byteBuffer.capacity()/2; System.out.println("to mcu datas: " + Arrays.toString(byteBuffer.array())); for (int i = 0; i &lt; capacity; i++) { // returnUnicode += "\\u" + Integer.toString((byteBuffer.get(2*i)&lt;&lt;8|(byteBuffer.get(2*i+1)&amp;0xff))&amp;0xffff, 16); returnUnicode += "\\u" + Integer.toHexString((byteBuffer.get(2*i)&lt;&lt;8|(byteBuffer.get(2*i+1)&amp;0xff)) &amp; 0xffff); } System.out.println("returnUnicode = " + returnUnicode); String revert = unicodeToCn(returnUnicode); System.out.println("revert = " + revert); return byteBuffer.array(); } </code></pre>

页面列表

ITEM_HTML