Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert CString to unsigned short in MFC?

Status
Not open for further replies.

Rmck87

Programmer
Jul 14, 2003
182
0
0
US
Hello, I am attempting to convert a CString variable (strTemp, which is coming from an edit control) into a WORD (m_cmd, aka unsigned short). I have been trying to figure this out for over a week now and have come to nothing.

I am familiar with sscanf and was using:

sscanf_s(strTemp, "%x", &m_cmd);

However, %x is used for DWORDS, and every time I would use this, it would overwrite whatever m_cmd was next to in the stack.

Thanks in advance!


One Ring to Rule Them All, One Ring to Find Them, One Ring to Bring Them All, and in the Darkness Bind Them.
 
Try
I am assuming that m_cmd is an unsigned short.
m_cmd = (unsigned short)atoi(strTemp);

 
What is wrong? Seems to me you said that it overwrote what was on the stack. That appears to be a problem (AKA wrong) to me. You are passing the address to an object probably an unsigned short (16 bits) and telling sscanf to fill it with a DWORD (64 bits). Seems pretty self explanitory on what the problem is.

Regards.
 
level2programmer said:
What is wrong? Seems to me you said that it overwrote what was on the stack. That appears to be a problem (AKA wrong) to me. You are passing the address to an object probably an unsigned short (16 bits) and telling sscanf to fill it with a DWORD (64 bits). Seems pretty self explanitory on what the problem is.

Regards.

I was talking to the OP, Rmck87. :)

There is another thread I linked to with several solutions (including yours), and I was wondering what was wrong with those that required him or her to start a new thread with the same question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top