Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3804
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2016-12-07 13:37
文章分类
文章存档

2016年(1)

我的朋友
最近访客

分类: Java

2016-12-07 13:40:21

题目:例如将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. }


阅读(745) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~