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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert CString 0-9 and a-f to hex value 1

Status
Not open for further replies.

tmoody

Technical User
Oct 8, 2001
37
US
I'm having a little trouble converting user input to it's hex equal. I have a DDV to validate a CString of 0-9 and a-f(or A-F), thanks to DaveTips, when the user types it in, but now I need to convert it to hex to send that value out a port. this seems pretty simple, but I am stumbling over it. After the user enters data I have a string, as an example, "5A". I need to convert that to an integer of 0x5A or 90 decimal. Thanks in advance
 
I do not remember currently any functions which can convert. If there is i do not know. But u can write a function on ur own which will convert a string to integer where the string is Hex notation. The code is below

CString str = "5BF";
unsigned int dec = 0;

str.MakeUpper();

for(int i = 0; i < str.GetLength() ; i++)
{
dec = (str.GetAt( strGetLength() - 1 - i ) - 'A' ) * (unsigned int) pow(16, i);
}

Hope it helps

raochetan
 
Maybe u can use the member funtion----CString::format().U can look it in MSDN for detail.
 
Sometimes there's a use for those oldtime C functions!

CString hex(&quot;0AF&quot;);
long l;
sscanf((const char*)hex,&quot;%x&quot;,&l);
:) Click here if this helped! :)
vvvvvvvvv
 
One more suggestion.

Strtol or strtoul with the CString as the first param, a char pointer as the second and 16 as the 3rd. The return value will be the hex value

matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top