Java转码问题 ASCII BCD

 public static void main(String[] args) throws IOException {  
        // TODO Auto-generated method stub  
        String s = "AD6DEC74E223517170487C238A7527B0DD0CA1C684FC13F666473E7C08323F9F66473E7C08323F9F66473E7C08323F9F66473E7C08323F9F66473E7 C08323F9F66473E7C08323F9F0000000000000000";  
        byte[] bcd = ASCII_To_BCD(s.getBytes(), s.length());  
        String s1 = bcd2Str(bcd);  
          
    }  
  
    private static byte asc_to_bcd(byte asc) {  
        byte bcd;  
  
        if ((asc >= '0') && (asc <= '9'))  
            bcd = (byte) (asc - '0');  
        else if ((asc >= 'A') && (asc <= 'F'))  
            bcd = (byte) (asc - 'A' + 10);  
        else if ((asc >= 'a') && (asc <= 'f'))  
            bcd = (byte) (asc - 'a' + 10);  
        else  
            bcd = (byte) (asc - 48);  
        return bcd;  
    }  
  
    private static byte[] ASCII_To_BCD(byte[] ascii, int asc_len) {  
        byte[] bcd = new byte[asc_len / 2];  
        int j = 0;  
        for (int i = 0; i < (asc_len + 1) / 2; i++) {  
            bcd[i] = asc_to_bcd(ascii[j++]);  
            bcd[i] = (byte) (((j >= asc_len) ? 0x00 : asc_to_bcd(ascii[j++])) + (bcd[i] << 4));  
        }  
        return bcd;  
    }  
  
    public static String bcd2Str(byte[] bytes) {  
        char temp[] = new char[bytes.length * 2], val;  
  
        for (int i = 0; i < bytes.length; i++) {  
            val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);  
            temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');  
  
            val = (char) (bytes[i] & 0x0f);  
            temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');  
        }  
        return new String(temp);  
    }

ASCII to BCD BCD to ASCII

代码如上:

还有其他转码

/ 整数到字节数组转换
 public static byte[] int2bytes(int n) {
  byte[] ab = new byte[4];
  ab[0] = (byte) (0xff & n);
  ab[1] = (byte) ((0xff00 & n) >> 8);
  ab[2] = (byte) ((0xff0000 & n) >> 16);
  ab[3] = (byte) ((0xff000000 & n) >> 24);
  return ab;
 }
 // 字节数组到整数的转换
 public static int bytes2int(byte b[]) {
  int s = 0;
  s = ((((b[0] & 0xff) << 8 | (b[1] & 0xff)) << 8) | (b[2] & 0xff)) << 8
    | (b[3] & 0xff);
  return s;
 }
 // 字节转换到字符
 public static char byte2char(byte b) {
  return (char) b;
 }
 private final static byte[] hex = "0123456789ABCDEF".getBytes();
 private static int parse(char c) {
  if (c >= 'a')
   return (c - 'a' + 10) & 0x0f;
  if (c >= 'A')
   return (c - 'A' + 10) & 0x0f;
  return (c - '0') & 0x0f;
 }
 // 从字节数组到十六进制字符串转换
 public static String Bytes2HexString(byte[] b) {
  byte[] buff = new byte[2 * b.length];
  for (int i = 0; i < b.length; i++) {
   buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
   buff[2 * i + 1] = hex[b[i] & 0x0f];
  }
  return new String(buff);
 }
 // 从十六进制字符串到字节数组转换
 public static byte[] HexString2Bytes(String hexstr) {
  byte[] b = new byte[hexstr.length() / 2];
  int j = 0;
  for (int i = 0; i < b.length; i++) {
   char c0 = hexstr.charAt(j++);
   char c1 = hexstr.charAt(j++);
   b[i] = (byte) ((parse(c0) << 4) | parse(c1));
  }
  return b;
 } 



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值