Java的String数字和BCD码相互转换

题目:例如将String="1234567890ABCDEF"转换为byte[]={0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF}
代码如下:

  1. public class javaHexStr 
  2. {
  3.     public static byte[] str2Bcd(String asc) { 
  4.         int len = asc.length(); 
  5.         int mod = len % 2; 
  6.         if (mod != 0) { 
  7.             asc = "0" + asc; 
  8.             len = asc.length(); 
  9.         } 
  10.         byte abt[] = new byte[len]; 
  11.         if (len >= 2) { 
  12.             len = len / 2; 
  13.         } 
  14.         byte bbt[] = new byte[len]; 
  15.         abt = asc.getBytes(); 
  16.         int j, k; 
  17.         for (int p = 0; p < asc.length() / 2; p++) { 
  18.             if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) { 
  19.                 j = abt[2 * p] - '0'; 
  20.             } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) { 
  21.                 j = abt[2 * p] - 'a' + 0x0a; 
  22.             } else { 
  23.                 j = abt[2 * p] - 'A' + 0x0a; 
  24.             } 
  25.             if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) { 
  26.                 k = abt[2 * p + 1] - '0'; 
  27.             } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) { 
  28.                 k = abt[2 * p + 1] - 'a' + 0x0a; 
  29.             } else { 
  30.                 k = abt[2 * p + 1] - 'A' + 0x0a; 
  31.             } 
  32.             int a = (j << 4) + k; 
  33.             byte b = (byte) a; 
  34.             bbt[p] = b; 
  35.             System.out.format("%02X\n", bbt[p]);
  36.         } 
  37.         return bbt; 
  38.     } 
  39.      private static byte asc_to_bcd(byte asc) { 
  40.      byte bcd; 
  41.      
  42.      if ((asc >= '0') && (asc <= '9')) 
  43.      bcd = (byte) (asc - '0'); 
  44.      else if ((asc >= 'A') && (asc <= 'F')) 
  45.      bcd = (byte) (asc - 'A' + 10); 
  46.      else if ((asc >= 'a') && (asc <= 'f')) 
  47.      bcd = (byte) (asc - 'a' + 10); 
  48.      else 
  49.      bcd = (byte) (asc - 48); 
  50.      return bcd; 
  51.      } 
  52.      
  53.      private static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) { 
  54.      byte[] bcd = new byte[asc_len / 2]; 
  55.      int j = 0; 
  56.      for (int i = 0; i < (asc_len + 1) / 2; i++) { 
  57.      bcd[i] = asc_to_bcd(ascii[j++]); 
  58.      bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++])) + (bcd[i] << 4)); 
  59.      System.out.format("%02X\n", bcd[i]);
  60.      } 
  61.      return bcd; 
  62.      } 
  63.      
  64.      public static String bcd2Str(byte[] bytes) { 
  65.      char temp[] = new char[bytes.length * 2], val; 
  66.      
  67.      for (int i = 0; i < bytes.length; i++) { 
  68.      val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f); 
  69.      temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); 
  70.      
  71.      val = (char) (bytes[i] & 0x0f); 
  72.      temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0'); 
  73.      } 
  74.      return new String(temp); 
  75.      }
  76.     /**
  77.      * @param args
  78.      */
  79.     public static void main(String[] args) {
  80.         // TODO Auto-generated method stub
  81.         //System.out.print("Hello,World!");
  82.          String s = "12345678123456781234567812345678"; 
  83.      byte[] bcd = ASCII_To_BCD(s.getBytes(), s.length()); 
  84.          //byte[] bcd = str2Bcd(s);
  85.      // String s1 = bcd2Str(bcd);
  86.      //System.out.print(s1);
  87.     }
  88.     
  89. }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值