下面两个函数的功能是把编辑框一的十六进制的字符串转换为十进制字符串并放入编辑框二中,
m_strHex2是与编辑框一相关联的控件变量,m_strDec2 是与编辑框二相关联的控件变量。问题:当我在编辑框1输入一个字符后(如a,第二个编辑框马上可以转换为十进制的10),就出现debug错误的问题,请问这是为什么,请高手指教 ?????另外:void CPageUser::HexToDec(char *string)这个函数在控制台下是没有问题的。 还有,当我去掉m_strDec2.ReleaseBuffer()这句时,弹出debug错误是在输入第二个字符后?????
void CPageUser::OnChangeEditHex() 
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CPropertyPage::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.

// TODO: Add your control notification handler code here
m_strDec2 = "";
m_wndEditHex.SetLimitText(12);

UpdateData(TRUE);
m_strDec2 = m_strHex2;
    HexToDec(m_strDec2.GetBuffer(m_strDec2.GetLength()));
UpdateData(FALSE);
m_strDec2.ReleaseBuffer();

}//把字符串表示的十六进制数转换为十进制数
void CPageUser::HexToDec(char *string)
{
char str[50];
char strTmp[3] = {0};
double Dec = 0;
double count = 0; unsigned len = strlen(string);
strcpy(str,string); while(len)
{
if (str[len-1] >='a')
{
strTmp[0] = '1';
strTmp[1] = '0' + str[len-1] - 'a';
}
else if (str[len-1] >='A')
{
strTmp[0] = '1';
strTmp[1] = '0' + str[len-1] - 'A';
}
else
{
strTmp[0] = str[len-1];
}

Dec += ((double)atoi(strTmp) * pow((double)16,count));
strTmp[1] = 0;
count++;
len--;
}
itoa((int)Dec,string,10);
}

解决方案 »

  1.   

    没读,不过给你个建议,CString的串用_T(""),另外要用CString::GetBuffer(0)~
      

  2.   

    首先,直接传递CStirng就可以了
    GetBuffer,ReleaseBuffer的麻烦,反正我是很少用
      

  3.   

    第二,你传入传出都是用的同一个参数
    比如你输入a,那长度是1,你m_strDec2.GetLength()的长度也是1
    但是返回的时候,成了10了,长度就是2了,就出现了越界
    这就是错误出现的原因
      

  4.   

    第三,比较简单的代码可以这么写:CString strTmp = "a";char* stopstring;
    int i = strtoul(strTmp, &stopstring, 16);strTmp.Format("%d", i);MessageBox(strTmp);