MFC c++ convert a CString to BYTE

Thread Starter

TBayBoy

Joined May 25, 2011
148
So if I understand correctly a BYTE is an unsigned character so here is the code I:

Rich (BB code):

void CEL505_Lab002Dlg::OnBnClickedButtonInput()
{
// TODO: Add your control notification handler code here
CString sBuffer;
unsigned char temp_num;
InputDlg input;
input.m_sHexInput.Format( _T("%02X"),m_bNumber);
if(input.DoModal() ==IDOK)
{

sBuffer.Format(_T("%s"), input.m_sHexInput);
temp_num.Format( _T("%u"),input.m_sHexInput);



//MessageBox(sBuffer,_T("Custom Dialog Result"),MB_OK | MB_ICONINFORMATION);
}

}
I push a BYTE to an input box as a Hex, and then after the user updates the number and presses OK the number updated value is returned as a CString.
sBuffer will hold the string, but I want temp_num to hold the returned string as a BYTE or unsigned character.

the problem seems to be "temp_num.Format( _T("%u"),input.m_sHexInput);"

wither I haven't given temp_num a class properly or I am going about this all wrong.

Help is greatly appreciated ;)
 

Tesla23

Joined May 10, 2009
560
So if I understand correctly a BYTE is an unsigned character so here is the code I:

Rich (BB code):

void CEL505_Lab002Dlg::OnBnClickedButtonInput()
{
// TODO: Add your control notification handler code here
CString sBuffer;
unsignedchar temp_num;
InputDlg input;
input.m_sHexInput.Format( _T("%02X"),m_bNumber);
if(input.DoModal() ==IDOK)
{
 
sBuffer.Format(_T("%s"), input.m_sHexInput);
temp_num.Format( _T("%u"),input.m_sHexInput);
 
 
 
//MessageBox(sBuffer,_T("Custom Dialog Result"),MB_OK | MB_ICONINFORMATION);
}
 
}
I push a BYTE to an input box as a Hex, and then after the user updates the number and presses OK the number updated value is returned as a CString.
sBuffer will hold the string, but I want temp_num to hold the returned string as a BYTE or unsigned character.

the problem seems to be "temp_num.Format( _T("%u"),input.m_sHexInput);"

wither I haven't given temp_num a class properly or I am going about this all wrong.

Help is greatly appreciated ;)
You seem to have confused a few things. Without seeing the declaration of InputDlg, is seems that m_sHexInput is a CString. In this case,

Rich (BB code):
sBuffer.Format(_T("%s"), input.m_sHexInput);
can be simply
Rich (BB code):
sBuffer = input.m_sHexInput;
and

Rich (BB code):
temp_num.Format( _T("%u"),input.m_sHexInput);
should be something like:

Rich (BB code):
temp_num = atoi(input.m_sHexInput);
This is poor code though as there is no error checking (the user could enter anything), and atoi will convert to an int and you are truncating to an unsigned char. This works for decimal input format, if you expect the input to be in hex format (0x...) then you need to investigate further.
 

Thread Starter

TBayBoy

Joined May 25, 2011
148
The final solution was (entered value was checked for errors proir to being passed back here):

Rich (BB code):

void CEL505_LabsDlg::OnBnClickedInput()
{
// TODO: Add your control notification handler code here
CString sBuffer;
InputDlg input;
input.m_sHexInput.Format( _T("%02X"),m_bNumber);
if(input.DoModal() ==IDOK)
{
 
sBuffer.Format(_T("%s"), input.m_sHexInput);
// convert the unicode to ASCII text
CT2A text (input.m_sHexInput);
// cast the ASCII HEX text to BYTE 
m_bNumber = (BYTE) strtoul (text.m_szBuffer,NULL,16);
//copy temp to global byte and update values.
//m_bNumber=bTempNum;
UpdateLEDs();
UpdateNumericEquivalents();
UpdateExternalLEDs();

// //MessageBox(sBuffer,_T("Custom Dialog Result"),MB_OK | MB_ICONINFORMATION);
}
}
 
Top