zoukankan      html  css  js  c++  java
  • android的二进制和十六进制的相互转换工具类(一):

    二进制和十六进制的相互转换工具类:

     1 package com.gzcivil.utils;
     2 
     3 
     4 public class BinStr {
     5     
     6     /**
     7      * 将二进制转换成16进制
     8      * @param buf
     9      * @return
    10      */
    11     private static char szbyte2str[][] = {
    12         {'0','0'},{'0','1'},{'0','2'},{'0','3'},{'0','4'},{'0','5'},{'0','6'},{'0','7'},
    13         {'0','8'},{'0','9'},{'0','A'},{'0','B'},{'0','C'},{'0','D'},{'0','E'},{'0','F'},
    14         
    15         {'1','0'},{'1','1'},{'1','2'},{'1','3'},{'1','4'},{'1','5'},{'1','6'},{'1','7'},
    16         {'1','8'},{'1','9'},{'1','A'},{'1','B'},{'1','C'},{'1','D'},{'1','E'},{'1','F'},
    17 
    18         {'2','0'},{'2','1'},{'2','2'},{'2','3'},{'2','4'},{'2','5'},{'2','6'},{'2','7'},
    19         {'2','8'},{'2','9'},{'2','A'},{'2','B'},{'2','C'},{'2','D'},{'2','E'},{'2','F'},
    20 
    21         {'3','0'},{'3','1'},{'3','2'},{'3','3'},{'3','4'},{'3','5'},{'3','6'},{'3','7'},
    22         {'3','8'},{'3','9'},{'3','A'},{'3','B'},{'3','C'},{'3','D'},{'3','E'},{'3','F'},
    23 
    24         {'4','0'},{'4','1'},{'4','2'},{'4','3'},{'4','4'},{'4','5'},{'4','6'},{'4','7'},
    25         {'4','8'},{'4','9'},{'4','A'},{'4','B'},{'4','C'},{'4','D'},{'4','E'},{'4','F'},
    26 
    27         {'5','0'},{'5','1'},{'5','2'},{'5','3'},{'5','4'},{'5','5'},{'5','6'},{'5','7'},
    28         {'5','8'},{'5','9'},{'5','A'},{'5','B'},{'5','C'},{'5','D'},{'5','E'},{'5','F'},
    29 
    30         {'6','0'},{'6','1'},{'6','2'},{'6','3'},{'6','4'},{'6','5'},{'6','6'},{'6','7'},
    31         {'6','8'},{'6','9'},{'6','A'},{'6','B'},{'6','C'},{'6','D'},{'6','E'},{'6','F'},
    32 
    33         {'7','0'},{'7','1'},{'7','2'},{'7','3'},{'7','4'},{'7','5'},{'7','6'},{'7','7'},
    34         {'7','8'},{'7','9'},{'7','A'},{'7','B'},{'7','C'},{'7','D'},{'7','E'},{'7','F'},
    35 
    36         {'8','0'},{'8','1'},{'8','2'},{'8','3'},{'8','4'},{'8','5'},{'8','6'},{'8','7'},
    37         {'8','8'},{'8','9'},{'8','A'},{'8','B'},{'8','C'},{'8','D'},{'8','E'},{'8','F'},
    38 
    39         {'9','0'},{'9','1'},{'9','2'},{'9','3'},{'9','4'},{'9','5'},{'9','6'},{'9','7'},
    40         {'9','8'},{'9','9'},{'9','A'},{'9','B'},{'9','C'},{'9','D'},{'9','E'},{'9','F'},
    41 
    42         {'A','0'},{'A','1'},{'A','2'},{'A','3'},{'A','4'},{'A','5'},{'A','6'},{'A','7'},
    43         {'A','8'},{'A','9'},{'A','A'},{'A','B'},{'A','C'},{'A','D'},{'A','E'},{'A','F'},
    44 
    45         {'B','0'},{'B','1'},{'B','2'},{'B','3'},{'B','4'},{'B','5'},{'B','6'},{'B','7'},
    46         {'B','8'},{'B','9'},{'B','A'},{'B','B'},{'B','C'},{'B','D'},{'B','E'},{'B','F'},
    47 
    48         {'C','0'},{'C','1'},{'C','2'},{'C','3'},{'C','4'},{'C','5'},{'C','6'},{'C','7'},
    49         {'C','8'},{'C','9'},{'C','A'},{'C','B'},{'C','C'},{'C','D'},{'C','E'},{'C','F'},
    50 
    51         {'D','0'},{'D','1'},{'D','2'},{'D','3'},{'D','4'},{'D','5'},{'D','6'},{'D','7'},
    52         {'D','8'},{'D','9'},{'D','A'},{'D','B'},{'D','C'},{'D','D'},{'D','E'},{'D','F'},
    53 
    54         {'E','0'},{'E','1'},{'E','2'},{'E','3'},{'E','4'},{'E','5'},{'E','6'},{'E','7'},
    55         {'E','8'},{'E','9'},{'E','A'},{'E','B'},{'E','C'},{'E','D'},{'E','E'},{'E','F'},
    56 
    57         {'F','0'},{'F','1'},{'F','2'},{'F','3'},{'F','4'},{'F','5'},{'F','6'},{'F','7'},
    58         {'F','8'},{'F','9'},{'F','A'},{'F','B'},{'F','C'},{'F','D'},{'F','E'},{'F','F'}
    59     };
    60     public static String byte2str(byte buf[]) {
    61         StringBuffer sb = new StringBuffer();
    62         for (int i = 0; i < buf.length; i++){
    63             sb.append(szbyte2str[buf[i]&0xFF][0]);
    64             sb.append(szbyte2str[buf[i]&0xFF][1]);
    65         }
    66         return sb.toString();
    67     }
    68     
    69     /**
    70      * 将16进制转换为二进制
    71      * @param hexStr
    72      * @return
    73      */
    74     private static char szstr2bin[] = {
    75         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,            0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    76         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,            0,1,2,3,4,5,6,7,8,9,10,0,0,0,0,0,
    77         0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    78         0,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0
    79     };
    80     public static byte[] str2byte(String str) {
    81         int length = str.length();
    82         if(length < 1)  return null;
    83         if(length%2 != 0)return null;
    84         byte[] result = new byte[str.length()/2];
    85         for (int i = 0;i<length;) {
    86             char H = szstr2bin[str.charAt(i++)&0xFF];
    87             char L = szstr2bin[str.charAt(i++)&0xFF];
    88             result[(i/2)-1] = (byte)(H*16 + L);
    89         }
    90         return result;
    91     }
    92 }
  • 相关阅读:
    程序猿和产品狗
    编写一个友元函数,求两个日期之间相差的天数
    集合的模拟实现(类模板)
    友元很简单2016final
    攻防世界-parallel-comparator-200
    web-GXYCTF2019 StrongestMind
    破解010Editor
    2020第三届江西省高校网络安全技能大赛线下解题赛部分wp
    pwn-EasyHeap(House of spirit)
    web-2020 ciscn littlegame(原型链污染)
  • 原文地址:https://www.cnblogs.com/lijinlun0825/p/5174650.html
Copyright © 2011-2022 走看看