Android 藍芽(Bluetooth)

來源:互聯網
上載者:User

標籤:

 

 

 

 

 

 

 

 

演算法 → byte數組 和 16進位進位見的轉換

http://javaman.group.iteye.com/group/wiki/3405-cuisuqiang-google-socket-InputStream-OutputStream

 

private final static byte[] hex = "0123456789ABCDEF".getBytes();private static int parse(char c) {    if (c >= ‘a‘)        return (c - ‘a‘ + 10) & 0x0f;    if (c >= ‘A‘)        return (c - ‘A‘ + 10) & 0x0f;    return (c - ‘0‘) & 0x0f;}// 從位元組數組到十六進位字串轉換public static String Bytes2HexString(byte[] b) {    byte[] buff = new byte[2 * b.length];    for (int i = 0; i < b.length; i++) {        buff[2 * i] = hex[(b[i] >> 4) & 0x0f];        buff[2 * i + 1] = hex[b[i] & 0x0f];    }    return new String(buff);}// 從十六進位字串到位元組數群組轉換public static byte[] HexString2Bytes(String hexstr) {    byte[] b = new byte[hexstr.length() / 2];    int j = 0;    for (int i = 0; i < b.length; i++) {        char c0 = hexstr.charAt(j++);        char c1 = hexstr.charAt(j++);        b[i] = (byte) ((parse(c0) << 4) | parse(c1));    }    return b;}public static void main(String[] args) {    byte[] bt = new byte[]{10, 2, 12, 14, 1, 0, 0, 1, 0, 31, 45, 1, 8, 0, 1, 0, -96, -45, 10, 3};    System.out.println(Bytes2HexString(bt));    System.out.println(Arrays.toString(HexString2Bytes("0A020C0E01000001001F2D0108000100A0D30A03")));}

列印

0A020C0E01000001001F2D0108000100A0D30A03[10, 2, 12, 14, 1, 0, 0, 1, 0, 31, 45, 1, 8, 0, 1, 0, -96, -45, 10, 3]

 

// 返回無符號的2進位表示 1110011String hex = Integer.toBinaryString(115);System.out.println(hex);// 返回2進位的字串1110011對應的值 115System.out.println(Integer.valueOf("1110011", 2));// 16進位值轉換成二進位System.out.println(Long.parseLong("49", 16));System.out.println(Long.parseLong("2F", 16));

 

Android 藍芽(Bluetooth)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.