zoukankan      html  css  js  c++  java
  • java中String十六进制Stringyte[]之间相互转换函数

    1. java二进制,字节数组,字符,十六进制,BCD编码转换2007-06-07 00:17/** *//**  
    2.     * 把16进制字符串转换成字节数组  
    3.     * @param hex  
    4.     * @return  
    5.     */   
    6. public static byte[] hexStringToByte(String hex) {   
    7.     int len = (hex.length() / 2);   
    8.     byte[] result = new byte[len];   
    9.     char[] achar = hex.toCharArray();   
    10.     for (int i = 0; i < len; i++) {   
    11.      int pos = i * 2;   
    12.      result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));   
    13.     }   
    14.     return result;   
    15. }  
    16.   
    17. private static byte toByte(char c) {   
    18.     byte b = (byte) "0123456789ABCDEF".indexOf(c);   
    19.     return b;   
    20. }  
    21.   
    22. /** *//**  
    23.     * 把字节数组转换成16进制字符串  
    24.     * @param bArray  
    25.     * @return  
    26.     */   
    27. public static final String bytesToHexString(byte[] bArray) {   
    28.     StringBuffer sb = new StringBuffer(bArray.length);   
    29.     String sTemp;   
    30.     for (int i = 0; i < bArray.length; i++) {   
    31.      sTemp = Integer.toHexString(0xFF & bArray[i]);   
    32.      if (sTemp.length() < 2)   
    33.       sb.append(0);   
    34.      sb.append(sTemp.toUpperCase());   
    35.     }   
    36.     return sb.toString();   
    37. }  
    38.   
    39. /** *//**  
    40.     * 把字节数组转换为对象  
    41.     * @param bytes  
    42.     * @return  
    43.     * @throws IOException  
    44.     * @throws ClassNotFoundException  
    45.     */   
    46. public static final Object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {   
    47.     ByteArrayInputStream in = new ByteArrayInputStream(bytes);   
    48.     ObjectInputStream oi = new ObjectInputStream(in);   
    49.     Object o = oi.readObject();   
    50.     oi.close();   
    51.     return o;   
    52. }  
    53.   
    54. /** *//**  
    55.     * 把可序列化对象转换成字节数组  
    56.     * @param s  
    57.     * @return  
    58.     * @throws IOException  
    59.     */   
    60. public static final byte[] objectToBytes(Serializable s) throws IOException {   
    61.     ByteArrayOutputStream out = new ByteArrayOutputStream();   
    62.     ObjectOutputStream ot = new ObjectOutputStream(out);   
    63.     ot.writeObject(s);   
    64.     ot.flush();   
    65.     ot.close();   
    66.     return out.toByteArray();   
    67. }  
    68.   
    69. public static final String objectToHexString(Serializable s) throws IOException{   
    70.     return bytesToHexString(objectToBytes(s));   
    71. }  
    72.   
    73. public static final Object hexStringToObject(String hex) throws IOException, ClassNotFoundException{   
    74.     return bytesToObject(hexStringToByte(hex));   
    75. }  
    76.   
    77. /** *//**  
    78.     * @函数功能: BCD码转为10进制串(阿拉伯数据)  
    79.     * @输入参数: BCD码  
    80.     * @输出结果: 10进制串  
    81.     */   
    82. public static String bcd2Str(byte[] bytes){   
    83.     StringBuffer temp=new StringBuffer(bytes.length*2);  
    84.   
    85.     for(int i=0;i<bytes.length;i++){   
    86.      temp.append((byte)((bytes[i]& 0xf0)>>>4));   
    87.      temp.append((byte)(bytes[i]& 0x0f));   
    88.     }   
    89.     return temp.toString().substring(0,1).equalsIgnoreCase("0")?temp.toString().substring(1):temp.toString();   
    90. }  
    91.   
    92. /** *//**  
    93.     * @函数功能: 10进制串转为BCD码  
    94.     * @输入参数: 10进制串  
    95.     * @输出结果: BCD码  
    96.     */   
    97. public static byte[] str2Bcd(String asc) {   
    98.     int len = asc.length();   
    99.     int mod = len % 2;  
    100.   
    101.     if (mod != 0) {   
    102.      asc = "0" + asc;   
    103.      len = asc.length();   
    104.     }  
    105.   
    106.     byte abt[] = new byte[len];   
    107.     if (len >= 2) {   
    108.      len = len / 2;   
    109.     }  
    110.   
    111.     byte bbt[] = new byte[len];   
    112.     abt = asc.getBytes();   
    113.     int j, k;  
    114.   
    115.     for (int p = 0; p < asc.length()/2; p++) {   
    116.      if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {   
    117.       j = abt[2 * p] - '0';   
    118.      } else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {   
    119.       j = abt[2 * p] - 'a' + 0x0a;   
    120.      } else {   
    121.       j = abt[2 * p] - 'A' + 0x0a;   
    122.      }  
    123.   
    124.      if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {   
    125.       k = abt[2 * p + 1] - '0';   
    126.      } else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {   
    127.       k = abt[2 * p + 1] - 'a' + 0x0a;   
    128.      }else {   
    129.       k = abt[2 * p + 1] - 'A' + 0x0a;   
    130.      }  
    131.   
    132.      int a = (j << 4) + k;   
    133.      byte b = (byte) a;   
    134.      bbt[p] = b;   
    135.     }   
    136.     return bbt;   
    137. }   
    138. /** *//**  
    139.     * @函数功能: BCD码转ASC码  
    140.     * @输入参数: BCD串  
    141.     * @输出结果: ASC码  
    142.     */   
    143. public static String BCD2ASC(byte[] bytes) {   
    144.     StringBuffer temp = new StringBuffer(bytes.length * 2);  
    145.   
    146.     for (int i = 0; i < bytes.length; i++) {   
    147.      int h = ((bytes[i] & 0xf0) >>> 4);   
    148.      int l = (bytes[i] & 0x0f);     
    149.      temp.append(BToA[h]).append( BToA[l]);   
    150.     }   
    151.     return temp.toString() ;   
    152. }  
    153.   
    154. /** *//**  
    155.     * MD5加密字符串,返回加密后的16进制字符串  
    156.     * @param origin  
    157.     * @return  
    158.     */   
    159. public static String MD5EncodeToHex(String origin) {   
    160.        return bytesToHexString(MD5Encode(origin));   
    161.      }  
    162.   
    163. /** *//**  
    164.     * MD5加密字符串,返回加密后的字节数组  
    165.     * @param origin  
    166.     * @return  
    167.     */   
    168. public static byte[] MD5Encode(String origin){   
    169.     return MD5Encode(origin.getBytes());   
    170. }  
    171.   
    172. /** *//**  
    173.     * MD5加密字节数组,返回加密后的字节数组  
    174.     * @param bytes  
    175.     * @return  
    176.     */   
    177. public static byte[] MD5Encode(byte[] bytes){   
    178.     MessageDigest md=null;   
    179.     try {   
    180.      md = MessageDigest.getInstance("MD5");   
    181.      return md.digest(bytes);   
    182.     } catch (NoSuchAlgorithmException e) {   
    183.      e.printStackTrace();   
    184.      return new byte[0];   
    185.     }   
    186.  
  • 相关阅读:
    SQL Server 2019安装及部署指南
    西门子1200PLC实用定位控制程序案例
    C#进行注册表项和键值操作
    上位机开发必备的一个实体类
    配置Internal Load balancer中VM的外网访问
    从中序后序遍历构造
    网络 | Linux ping任何ip均出现 Destination Host Unreachable 排查思路与方法
    Spring、Spring Framework、Spring Boot、Spring Cloud的区别
    Linux软件安装目录分类讲解
    APP嵌入H5时,软键盘处理(IOS)
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6529905.html
Copyright © 2011-2022 走看看