CString temporary[2],temporary[3];  //temporary[2]为08,temporary[3]为01 
......进行位移操作......             //期望得到结果为0108,并转换成int型十进制数264
因为CString是8位的  
所以  temporary[2] | (temporary[3] <<8)不可行
CString是一个类 不是基本形  所以又不能强制转换(int)temporary[3]
如果先将int hig=(atoi(temporary[3]))<<8;
       int low=atoi(temporary[2]);
       hig=hig | low;
得到结果又不是期待的0108所以请各位大侠帮忙 将两个CString合并成一个(temporary[2]低8位 temporary[3]高8位)并转换为int十进制数

解决方案 »

  1.   

    不太明白你的意思,移位是对整数来说的,CString是字符串阿,哪来的移位。
    大概你的意思是先转换成int类型,再进行移位,移好之后在格式化成CString
      

  2.   

    temporary总是2位的话,不如以16进制串先转成整数,再加上temporary以16进制串先转成整数再乘以256
      

  3.   

    CString temporary[2],temporary[3];  是定义吗?两个同样名称数组变量,严重错误
    估计你是想十六进制的字符串转成十进制的整数
    两个字符串直接加起来成新字符串就是0108了CString temp = temporary3 + temporary2;
    int result = strtol(temp, NULL, 16);
      

  4.   

    不是定义的两个同样的
    是定义一个CString temporary[]的数组   那两个是其中的数据
    我按你那样做的试过 原本以为是可以的
    不过有问题 才不得已用的位移操作
      

  5.   

    这两个变量是串口接收过来的字符(十六进制模式)
    中间怎么转换是我想请教的   最后我想要的是将2个8位的变量(A和B)组合成一个16位的变量其中A为高8位B为低8位
      
      

  6.   

    能说得详细点么?
    类似int hig=(atoi(temporary[3])) < <8; 
          int low=atoi(temporary[2]); 做么?
    然后呢 怎么组合成一个数?呢
      

  7.   

    你的问题跟我的情况一样,下面是一个高手指导我的程序,你看一下应该对你有用
    LONG CSerialPortTestDlg::OnComm(WPARAM ch, LPARAM port)
    {
    UINT  temp;
    // UINT Hbyte,Lbyte;这个应定义为全局变量
    //INT i=0;
    i++;
    if (i==1)
    {
    Hbyte=ch;高八位
    }
    if (i==2)
    {

    Lbyte=ch;
            temp = Hbyte;
            temp = temp << 8;   //高八位
            temp |= Lbyte;         //低八位
            CString hexCh;
            hexCh.Format(TEXT("%d "), temp);
            m_strEditReceiveMsg += hexCh;
    i=0;
            
    }
    UpdateData(FALSE);
    return 0;