Click here to Skip to main content
15,436,489 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in MFC, not much experience in C++ either.
I am trying to send data in edit box to serial port. I can send strings(LPCSTR) to WriteFile function as of now. Now I need to send data which user types in hex format in editbox to serial port. When I type say 0X6A or $6A i want to send 6A in hex otherwise as a string.

Can anybody help please. :confused:
Updated 13-Jun-10 23:51pm
Member 9336330 3-Sep-12 2:05am     CRLF
i am new in mfc i want to make a dailog based application using mfc in which i will enter the data to convert it into hexadecimal using button... i am entering the the data but it is not getting converted.. please help me send the code to

Getting a byte from its hexadecimal representation (string) is an almost trivial task, you may do it yourself or you may use, as suggested, strtoul [^] function or sscanf [^] one.
Share this answer
You could convert your input string using strtoint, it accepts decimal or hex numbers, here's a link to the msdn help:-;k(DevLang-"C++")&rd=true[^]

Share this answer
Gautam Raiker 14-Jun-10 5:56am     CRLF
Thanks Alison :) I think this function would help me i will give it a try. i would need more your help later.
LittleYellowBird 14-Jun-10 5:58am    
Glad to help :-)
If you want a C++ solution then consider using a stringstream and the std::hex manipulator.

For example to convert a string of hex digits into a number:
std::string hex( "6a204687" );
std::stringstream str( hex );
unsigned number = 0;
str >> std::hex >> number;

One other thought, you might like to implement a DDX_hex function to complement the other DDX_ functions MFC gives you for exchanging hex numbers with controls in a dialogue box.



Edited to get rid of a spurious word in the code.
Share this answer
Gautam Raiker 15-Jun-10 4:16am     CRLF
Thanks for your help. Could u tell me more about how to implement this DDX_Hex function .some links would be helpful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900