Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to convert Hexadecimal value in CString to Decimal value?
Posted

you can use the strtol[^] function. Specify the base as 16 for hex.

C++
CString myString = "FA"; //0xFA = 250
long myNumber = strtol((LPCSTR)myString, NULL, 16); //We dont care where the end of the string was for this example, so the 2nd param is NULL
//myNumber = 250;
 
Share this answer
 
Comments
Gokulnath007 16-Nov-11 3:13am    
I have the value "50B" in mystring. But while converting, my long variable is showing 0x00000050B itself. But the output should be 1291. How to get this output??
Andrew Brock 17-Nov-11 5:04am    
How are you displaying it?
printf("%d") for int/long
if it is the debugger, right click on it and untick "hexadecimal display"
if it isn't any of these either tell me and I can tell you how do display in base 10, or consult the documentation
long num = strtol((LPCSTR)myString, NULL, 16);
CString rev;
rev.Format(_T("%d"),num);

Now the value 1291 is there in the Cstring rev.
 
Share this answer
 

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