Question

I can convert strings to hex perfectly fine if I define them in the source for example:

int input = 15;
CString output;
output.Format(L"%x", input);
m_oput.SetWindowText(output);

The result is 'f', but how come if I do this:

TCHAR buffer[500];
int input = GetDlgItemText(TOCON, buffer, 50);
CString output;
output.Format(L"%x", input);
m_oput.SetWindowText(output);

The output is '2'? I have tried a few other ways of getting text from the edit control, but each of them have returned the wrong results. Is there something wrong with how I am getting text from the control in general, or what I have set for maxcount?

Was it helpful?

Solution

GetDlgItemText copies the text form the edit control into buffer and returns the number of characters read from dialog control, not the text interpreted as a number. The way to go is to first convert the string in buffer to an integer and then supply this to output.Format. For example, you can do this with the function atoi. Your modified code would look like this:

TCHAR buffer[500];
GetDlgItemText(TOCON, buffer, 50);
int input = atoi(buffer);
CString output;
output.Format(L"%x", input);
m_oput.SetWindowText(output);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top