将一个10进制的CString buffer= 832转换成16进制的CString temp = 340
该如何转换呢?
 printf("%x",xx);?
这样可以不?感谢~

解决方案 »

  1.   

    CString str = "832";
    int n = atoi(str);
    CString str1;
    str1.Format("0x%x",n);
    AfxMessageBox(str1);
      

  2.   

    CString buffer= 832;
    int num = atoi(buffer);
    temp.Format("%x",num);
      

  3.   

    CString sVal = "832";
    int n = atoi(sVal);
    CString sRVal;
    sRVal.Format("%x",n);
      

  4.   

    CString convert,strposx,strposy;;
    int posx,posy;
    BYTE Buffer[0x38] = {0};
    pGenTwDlg->pDebugDlg->m_PosxEdit.GetWindowText(strposx);
    posx = atoi((char*)(LPCTSTR)strposx);
            convert.Format(_T("%x"),posx*0x40);
    memcpy_s(Buffer+0x10,0x02,convert,0x02);
    pGenTwDlg->pDebugDlg->m_PosyEdit.GetWindowText(strposy);
    posy = atoi((char*)(LPCTSTR)strposy);
    convert.Format(_T("%x"),posy*0x40);
    memcpy_s(Buffer+0x14,0x02,convert,0x02);
    memcpy_s(Buffer+0x18,0x02,convert,0x02);为什么不对不对呢,我输入一个10进制的值 32时即(0x20),获取控件值后再乘以64,然后转化为16进制
    但是结果总是不对,结果老是显示40...
     调了半天也不知道是怎么回事
    如果我输入一个20时,Posx得到的值就变成02了。。不明白他是怎么弄得.
      

  5.   

    convert.Format(_T("%x"),posx*0x40);
    加断点,看下convert
      

  6.   

    永远都是40。。
    pGenTwDlg->pDebugDlg->m_PosyEdit.GetWindowText(strposy);
    posy = atoi((char*)(LPCTSTR)strposy);
    获取的控制值是对的,但是转换完赋给posy就不对了
      

  7.   

    你把调试过程中,posx和convert的值贴出来。
      

  8.   

    我好想知道问题出在哪里了。
    posy = atoi((char*)(LPCTSTR)strposy);
    格式转换的时候用(char*)的时候它总是取数组的第一个值。。
     如果输入20,它就取2
    如果输入120,它就取1
    这个可怎么办呢。
      

  9.   

    (char*)(LPCTSTR)可以都去掉,不过这个是问题所在吗?
      

  10.   

    atoi的参数需要一个const char*,
    难道我还要去重新定义类型。。
    转换的代码看的乱乱的
      

  11.   

    不行的啦。
    error C2664: “atoi”: 不能将参数 1 从“CString”转换为“const char *”
      

  12.   

    posx = atoi((char*)(LPCTSTR)strposx);
    改成posx = atoi(strposx);不需要进行复杂的转换。
      

  13.   

    额,分不是重要的。
     他们助人为乐顺便得分,主要的是能帮得到我。
    我这个设置的字符集的问题
    像convert.Format(_T("%x"),posy*0x40);都需要_T宏的
    所以posx = atoi(strposx);这句编辑都过不去的
     error C2664: “atoi”: 不能将参数 1 从“CString”转换为“const char *” 
      

  14.   

    char* pstr = strposx.GetBuffer(strposx.GetLength());
    posx = atoi(pstr );
    如果改成这样子又提示我字符集的问题 WTchar_T* to char *
      

  15.   

    char * CDlg::w2c(wchar_t * s)
    {
    int iSize; 
    char* pszMultiByte;
    iSize = WideCharToMultiByte(CP_ACP, 0, s, -1, NULL, 0, NULL, NULL); 
    pszMultiByte = new char[iSize+1]; 
    WideCharToMultiByte(CP_ACP, 0, s, -1, pszMultiByte, iSize, NULL, NULL); 
    return pszMultiByte;
    }
    char* pstr = w2c(strposx.GetBuffer());
    posx = atoi(pstr);
    这样就可以了
    再次感谢大家在百忙之中抽出时间来解答我的问题。