String s = "B510610E627D9E5A";
    在JAVA中如何将字符串转换成压缩的BCD码?

解决方案 »

  1.   

    void BcdToAscii (char *ascii_buf, const BYTE *bcd_buf, int len)
    {
     int i;
     char ch;
     
     for (i=0; i<len; i++)
     {
      if (i & 1) ch = *(bcd_buf++) & 0x0f;
      else ch = *bcd_buf >> 4;
      ascii_buf[i] = ch + ((ch > 9)? 'A'-10 : '0');
     }
    }
      

  2.   

    请高手看看我写的代码有没有问题?生成的BCD码是不是正确的?
      public static byte[] AcsToBcd(String asc) {    int len = asc.length();
        int mod = len % 2;
        if (mod != 0) {
          asc = "0" + asc;
          len = asc.length();
        }
        byte abt[] = new byte[len];
        if (len >= 2) {
          len = len / 2;
        }
        byte bbt[] = new byte[len];
        abt = asc.getBytes();
        int j, k;    for (int p = 0; p < asc.length()/2; p++) {
          if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
            j = abt[2 * p] - '0';
          }
          else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
            j = abt[2 * p] - 'a' + 0x0a;
          }
          else {
            j = abt[2 * p] - 'A' + 0x0a;
          }      if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
            k = abt[2 * p + 1] - '0';
          }
          else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
            k = abt[2 * p + 1] - 'a' + 0x0a;
          }
          else {
            k = abt[2 * p + 1] - 'A' + 0x0a;
          }
          //
          int a = (j << 4) + k;
          byte b = (byte) a;
          bbt[p] = b;
        }
        return bbt;
      }
      

  3.   

    请解释一下BSD的概念.---初学者
      

  4.   

    public class T
    {
    public static byte[] ByteToBCD(byte[] in)
    {
    byte[] out = new byte[in.length * 2];
    int tmp;
         for(int i=0; i< in.length * 2; i++)
    {
             tmp  = (in[i >> 1 ] >> ((i + 1) % 2)*4) & 0x0f ; switch(tmp)
    {
    case 10: out[i] = 'A'; break;
    case 11: out[i] = 'B'; break;
    case 12: out[i] = 'C'; break;
    case 13: out[i] = 'D'; break;
    case 14: out[i] = 'E'; break;
    case 15: out[i] = 'F'; break;
    default :
    out[i] = (byte)(tmp|0x30);
    }
    }

    return out;
    }

    public static void main(String[] args)
    {
    // 字符串这样传入
    //String src1 = "B510610E627D9E5A";
    //byte[] des = ByteToBCD(src.getBytes());

    byte[] src2 = {'1','2',(byte)0x1E};

    byte[] des = ByteToBCD(src);


    for (int i = 0; i < des.length; i ++)
    System.out.print((char)des[i] + " ");
    }
    }