Android與單片機通訊常用資料轉換方法(匯總)

來源:互聯網
上載者:User

標籤:

下面直接貼代碼

1.  將GB2312轉化為中文,如BAFAC2DCB2B7→胡蘿蔔,兩個位元組合成一個文字

    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.將中文轉化為GB2312,並且結果以byte[]形式返回,如胡蘿蔔→new byte[]{BA  FA C2 DC B2 B7},一個字被分為兩個位元組

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

 3.將十六進位的byte[]原封不動的轉化為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.將十六進位的byte[]原封不動的轉化為string,並且每個byte之間用空格分開,如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[]數組轉化為8、10、16等各種進位,例如byte[0x11,0x20]→4384,約等於1120(16進位)→4384,radix代表進位

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

6.將String的十六進位原封不動轉化為byte的十六進位,例如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;    }

 

Android與單片機通訊常用資料轉換方法(匯總)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.