国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android與單片機(jī)通信常用數(shù)據(jù)轉(zhuǎn)換方法總結(jié)

2019-12-12 05:19:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Android與單片機(jī)通信常用數(shù)據(jù)轉(zhuǎn)換方法

  1.  將GB2312轉(zhuǎn)化為中文,如BAFAC2DCB2B7→胡蘿卜,兩個(gè)字節(jié)合成一個(gè)文字

public static String stringToGbk(String string) throws Exception {   byte[] bytes = new byte[string.length() / 2];   for (int j = 0; j < bytes.length; j++) {     byte high = Byte.parseByte(string.substring(j * 2, j * 2 + 1), 16);     byte low = Byte.parseByte(string.substring(j * 2 + 1, j * 2 + 2),         16);     bytes[j] = (byte) (high << 4 | low);   }   String result = new String(bytes, "GBK");   return result; } 

  2.將中文轉(zhuǎn)化為GB2312,并且結(jié)果以byte[]形式返回,如胡蘿卜→new byte[]{BA  FA C2 DC B2 B7},一個(gè)字被分為兩個(gè)字節(jié)

public static byte[] gbkToString(String str) throws Exception {   return new String(str.getBytes("GBK"), "gb2312").getBytes("gb2312"); } 

  3.將十六進(jìn)制的byte[]原封不動(dòng)的轉(zhuǎn)化為string,如byte[]{0x7e,0x80,0x11,0x20}→7e801120,可用于log打印

public static String bytesToHexString(byte[] src) {   StringBuilder stringBuilder = new StringBuilder("");   if (src == null || src.length <= 0) {     return null;   }   for (int i = 0; i < src.length; i++) {     int v = src[i] & 0xFF;     String hv = Integer.toHexString(v);     if (hv.length() < 2) {       stringBuilder.append(0);     }     stringBuilder.append(hv);   }   return stringBuilder.toString(); } 

  4.將十六進(jìn)制的byte[]原封不動(dòng)的轉(zhuǎn)化為string,并且每個(gè)byte之間用空格分開(kāi),如byte[]{0x7e,0x80,0x11,0x20}→7e 80 11 20,,可用于log打印

public static StringBuilder byte2HexStr(byte[] data) {    if (data != null && data.length > 0) {     StringBuilder stringBuilder = new StringBuilder(data.length);     for (byte byteChar : data) {       stringBuilder.append(String.format("%02X ", byteChar));     }     return stringBuilder;   }   return null; } 

  5.將byte[]數(shù)組轉(zhuǎn)化為8、10、16等各種進(jìn)制,例如byte[0x11,0x20]→4384,約等于1120(16進(jìn)制)→4384,radix代表進(jìn)制

public static String bytesToAllHex(byte[] bytes, int radix) {   return new BigInteger(1, bytes).toString(radix);// 這里的1代表正數(shù) } 

  6.將String的十六進(jìn)制原封不動(dòng)轉(zhuǎn)化為byte的十六進(jìn)制,例如7e20→new byte[]{0x7e,x20}

public static byte[] HexString2Bytes(String src) {   byte[] ret = new byte[src.length() / 2];   byte[] tmp = src.getBytes();   for (int i = 0; i < tmp.length / 2; i++) {     ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);   }   return ret; } 
public static byte uniteBytes(byte src0, byte src1) {   byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))       .byteValue();   _b0 = (byte) (_b0 << 4);   byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))       .byteValue();   byte ret = (byte) (_b0 ^ _b1);   return ret; } 

以上就是對(duì)Android 與單片機(jī)通信的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料謝謝大家對(duì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 五常市| 庆阳市| 福鼎市| 文山县| 炉霍县| 晋宁县| 华坪县| 永仁县| 杨浦区| 文成县| 鄂伦春自治旗| 合山市| 阆中市| 阿尔山市| 奎屯市| 宜良县| 射阳县| 柘城县| 平江县| 甘泉县| 藁城市| 三河市| 铜山县| 灯塔市| 且末县| 镇康县| 云和县| 黔西| 垫江县| 高唐县| 闸北区| 崇信县| 无为县| 改则县| 上高县| 钟祥市| 乌拉特前旗| 磐安县| 达拉特旗| 大厂| 邛崃市|