简体   繁体   中英

c++ MFC converting a string in an edit control to hexidecimal

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM