Android與單片機(jī)通信常用數(shù)據(jù)轉(zhuǎn)換方法總結(jié)
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ì)本站的支持!
- C語(yǔ)言宏定義結(jié)合全局變量的方法實(shí)現(xiàn)單片機(jī)串口透?jìng)髂J?/a>
- android實(shí)現(xiàn)手機(jī)與單片機(jī)藍(lán)牙模塊通信
- Android Socket 線程連接openwrt與arduino單片機(jī)串口雙向通信的實(shí)例解析
- Android單片機(jī)與藍(lán)牙模塊通信實(shí)例代碼
- 使用UART與PC通信實(shí)現(xiàn)msp430g2553單片機(jī)超聲波測(cè)距示例
- c#實(shí)現(xiàn)51單片機(jī)頻率計(jì)的代碼分享(數(shù)字頻率計(jì)設(shè)計(jì))
- 關(guān)于單片機(jī)按鍵問(wèn)題性能提升總結(jié)
相關(guān)文章
android AudioRecorder簡(jiǎn)單心得分享
這篇文章介紹了android AudioRecorder簡(jiǎn)單心得,有需要的朋友可以參考一下2013-10-10
Android中TelephonyManager用法實(shí)例
這篇文章主要介紹了Android中TelephonyManager用法,結(jié)合實(shí)例形式分析了TelephonyManager類(lèi)的功能,使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03
android實(shí)現(xiàn)程序自動(dòng)升級(jí)到安裝示例分享(下載android程序安裝包)
這篇文章主要介紹了android實(shí)現(xiàn)下載android程序安裝包自動(dòng)升級(jí)的示例,大家參考使用吧2014-01-01
Android 擴(kuò)大 View 的點(diǎn)擊區(qū)域的方法
這篇文章主要介紹了Android 擴(kuò)大 View 的點(diǎn)擊區(qū)域的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Android實(shí)現(xiàn)自動(dòng)截圖腳本
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)截圖腳本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解
這篇文章主要介紹了在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解,消息推送功能可以說(shuō)移動(dòng)APP不可缺少的功能之一,使用WebSocket實(shí)現(xiàn)消息推送功能。感興趣的可以了解一下2020-07-07
Android Activity Results API代替onActivityResul
說(shuō)到onActivityResult,我們已經(jīng)非常熟悉來(lái),通過(guò)在A activity啟動(dòng)B activity并且傳入數(shù)據(jù)到B中,然后在A中通過(guò)onActivityResult來(lái)接收B中返回的數(shù)據(jù)。在最新的activity-ktx的beta版本中,谷歌已經(jīng)廢棄了onActivityResult2022-09-09
Android ActionBarActivity設(shè)置全屏無(wú)標(biāo)題實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android ActionBarActivity設(shè)置全屏無(wú)標(biāo)題實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04

