将下面的十六进制的BCD码转换为真正的十进制数:
  十六进制:12 34 56 78(分别在四个八位的字节中存放) 
      转换:12345678 (要求在一个DWORD型变量中存放)

解决方案 »

  1.   

    union myint
    {
    BYTE a1[4];
    int b;
    };union mydword
    {
    int b1;
    DWORD c;
    };CString CDialogDlg::ConvertASC2(BYTE a, BYTE b)
    {
    myint myunion;
    CString str;
    BYTE high, low, data[4];
    myunion.a1[0]=myunion.a1[1]=myunion.a1[2]=myunion.a1[3]=0x00;
    myunion.a1[0]=a;
    high=myunion.a1[0]&0xF0;
    high=high>>4;
    high=high&0x0F;
    data[0]=high;
    low=myunion.a1[0]&0x0F;
    data[1]=low;
    myunion.a1[0]=b;
    high=myunion.a1[0]&0xF0;
    high=high>>4;
    high=high&0x0F;
    data[2]=high;
    low=myunion.a1[0]&0x0F;
    data[3]=low;
    str.Format("%d%d%d%d", data[0], data[1], data[2], data[3]);
    return str;
    }BYTE a[4];   // to load BCD
    mydword test;  // to load ASC2
    CString str, str1, str2;
    DWORD result;
    a[0]=0x12;
    a[1]=0x34;
    a[2]=0x56;
    a[3]=0x78;
    str1=ConvertASC2(a[0], a[1]);
    str2=ConvertASC2(a[2], a[3]);
    str=str1+str2;
    test.b1=atoi(str);
    result=test.c;
    result应该是你要的结果!!!
    不知道对不对,你看看吧!!!
      

  2.   

    上面的方法太累了,这个方法好像也可以呀!!!
    union mydword
    {
    int b1;
    DWORD c;
    };BYTE a[4];   // to load BCD
    CString str;
    mydword test;
    DWORD result;
    a[0]=0x12;
    a[1]=0x34;
    a[2]=0x56;
    a[3]=0x78;
    str.Format("%x%x%x%x", a[0], a[1], a[2], a[3]);
    test.b1=atoi(str);
    result=test.c;
      

  3.   

    #define HEXASCII(val) ((val)>0 && (val)<10)?'0'+(val):\
                          ((val)>9 && (val)<16)?'A'+(val)-10:\
                          '0'
    void CCharDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    char buf[]={0x12,0x34,0x56,0x78,'\0'};
        CString temp=buf;
    CString temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp8;
    //HEXASCII
        temp1=HEXASCII((temp[0]&0xf0)>>4);
    temp2=HEXASCII(temp[0]&0x0f);
    temp3=HEXASCII((temp[1]&0xf0)>>4);
    temp4=HEXASCII(temp[1]&0x0f);
    temp5=HEXASCII((temp[2]&0xf0)>>4);
    temp6=HEXASCII(temp[2]&0x0f);
    temp7=HEXASCII((temp[3]&0xf0)>>4);
    temp8=HEXASCII(temp[3]&0x0f);
    CString result1=(temp1+temp2+temp3+temp4+temp5+temp6+temp7+temp8);
    //再把result1转化成DWORD型的........
    }
      

  4.   

    如果没有CString怎么办,也可以向下面这样:
    union mydword
    {
    int b1;
    DWORD c;
    };BYTE a[4];   // to load BCD
    char str[4];
    mydword test;
    DWORD result;a[0]=0x12;
    a[1]=0x34;
    a[2]=0x56;
    a[3]=0x78;
    memset(str, 0, 4);
    sprintf(str, "%x%x%x%x", a[0], a[1], a[2], a[3]);
    test.b1=atoi(str);
    result=test.c;