Android

个人Android学习总结


JAVA 数据类型转换

<pre><code class="language-java">package com.pdp.utils; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; import java.util.Arrays; public class BasicConvertUtil { //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; } /** * @功能 字节的转换与短整型 * @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(); } 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 += (char) Integer.valueOf(strs[i], 16).intValue(); } return returnStr; } public static String cnToUnicode(String cn) { char[] chars = cn.toCharArray(); 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; } public static String cnToUnicodeHex(String cn) { char[] chars = cn.toCharArray(); String returnStr = ""; for (int i = 0; i &lt; chars.length; i++) { // returnStr += Integer.toString(chars[i], 16); returnStr += "\\u" + Integer.toHexString(chars[i]); } return returnStr; } /** * 将字符串转化为 unicode码字节 * @param cn * @return */ public static int[] unicodeToIntArray(String cn){ char[] chars = cn.toCharArray(); IntBuffer buffer = IntBuffer.allocate(2*chars.length); for (int i = 0; i &lt; chars.length; i++) { // String unicode = Integer.toString(chars[i], 16); String unicode = Integer.toHexString(chars[i]); int value = Integer.valueOf(unicode,16).intValue(); //value 保存 2个字节 buffer.put(value &gt;&gt; 8); buffer.put(value &amp; 0xff); } return buffer.array(); } /** * 将字符串转化为 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