栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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("%02Xn", 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("%02Xn", 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.     public static void main(String[] args) {
  78.         // TODO Auto-generated method stub
  79.         //System.out.print("Hello,World!");
  80.          String s = "12345678123456781234567812345678"; 
  81.      byte[] bcd = ASCII_To_BCD(s.getBytes(), s.length()); 
  82.          //byte[] bcd = str2Bcd(s);
  83.      // String s1 = bcd2Str(bcd);
  84.      //System.out.print(s1);
  85.     }
  86.     
  87. }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/344918.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号