码迷,mamicode.com
首页 > 其他好文 > 详细

BCD编码

时间:2017-12-13 14:44:44      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:获取   class   param   package   static   bcd   copy   else   isp   

  1 package unit;
  2 
  3 import java.util.Arrays;
  4 
  5 /**
  6  * @author lujiayun
  7  * BCD编码工具类
  8  */
  9 public class BcdUtil {
 10 
 11     private BcdUtil(){}
 12 
 13     /**
 14      * 获取时间戳分散参数
 15      * @param timeStamp  时间戳
 16      * @return           byte[]
 17      */
 18     public static byte[] getDispersionParameter(String timeStamp){
 19         byte b1 = (byte) 0x80;
 20         byte b2 = (byte)0x00;
 21         return append(string2Bcd(timeStamp), b1, b2);
 22     }
 23 
 24     /**
 25      * 拼接字节数组至16字节
 26      * @param byteArray 原始字节数字
 27      * @param b1 需要拼接的字节
 28      * @param b2 需要拼接的字节
 29      * @return 16个字节的字节数组
 30      */
 31     private static byte[] append(byte[] byteArray, byte b1, byte b2){
 32         byte[] array = null;
 33         if(byteArray.length < 16){
 34             array = Arrays.copyOf(byteArray, 16);
 35             array[byteArray.length] = b1;
 36             for(int i = byteArray.length + 1; i < 16; i++){
 37                 array[i] = b2;
 38             }
 39 
 40         }
 41         return array;
 42     }
 43 
 44     /**
 45      * 将十进制字符串进行BCD编码
 46      * @param asc 原始字符串
 47      * @return 编码后的字节数组
 48      */
 49     private static byte[] string2Bcd(String asc) {
 50         int len = asc.length();
 51         int mod = len % 2;
 52 
 53         if (mod != 0) {
 54             asc = "0" + asc;
 55             len = asc.length();
 56         }
 57 
 58         if (len >= 2) {
 59             len = len / 2;
 60         }
 61 
 62         byte bbt[] = new byte[len];
 63         byte[] abt = asc.getBytes();
 64         int j, k;
 65 
 66         for (int p = 0; p < asc.length()/2; p++) {
 67             if ( (abt[2 * p] >= ‘0‘) && (abt[2 * p] <= ‘9‘)) {
 68                 j = abt[2 * p] - ‘0‘;
 69             } else if ( (abt[2 * p] >= ‘a‘) && (abt[2 * p] <= ‘z‘)) {
 70                 j = abt[2 * p] - ‘a‘ + 0x0a;
 71             } else {
 72                 j = abt[2 * p] - ‘A‘ + 0x0a;
 73             }
 74 
 75             if ( (abt[2 * p + 1] >= ‘0‘) && (abt[2 * p + 1] <= ‘9‘)) {
 76                 k = abt[2 * p + 1] - ‘0‘;
 77             } else if ( (abt[2 * p + 1] >= ‘a‘) && (abt[2 * p + 1] <= ‘z‘)) {
 78                 k = abt[2 * p + 1] - ‘a‘ + 0x0a;
 79             }else {
 80                 k = abt[2 * p + 1] - ‘A‘ + 0x0a;
 81             }
 82             int a = (j << 4) + k;
 83             byte b = (byte) a;
 84             bbt[p] = b;
 85         }
 86         return bbt;
 87     }
 88 
 89     /**
 90      * BCD字节数组转字符串
 91      * @param bytes 原字节数组
 92      * @return 字符串
 93      */
 94     public static String bcd2String(byte[] bytes) {
 95         StringBuilder temp = new StringBuilder(bytes.length * 2);
 96         for (byte aByte : bytes) {
 97             temp.append((byte) ((aByte & 0xf0) >>> 4));
 98             temp.append((byte) (aByte & 0x0f));
 99         }
100         return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp
101                 .toString().substring(1) : temp.toString();
102     }
103 
104 
105 }

 

BCD编码

标签:获取   class   param   package   static   bcd   copy   else   isp   

原文地址:http://www.cnblogs.com/redhat0019/p/8031925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!