Converting a Hex CString to Decimal CString

Hello.

I know there are many resources available for this question but they all don't seem to work for me for some reason.

I am trying to convert a CString representing a hex number into a CString containing a decimal number.

For example:

CString hexstring = 07BE;
CString decimalstring;

I want to convert 07BE to 1982.

If anyone can help, will be greatly appreciated!

Thank you in advance.
LVL 1
mr_stevieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
The easiest way would probably be 'strtol()' (or '_tcstol()' respectively) along with 'CString::Format()' then, e.g.
#include <stdlib.h>

//...

CString HexStrToDecStr(const CString& sHex) {

  CString sDec;
  char *p;
  sDec.Format("%d",_tcstol(sHex,&p,16));

  return sDec;
}

Open in new window

0
mr_stevieAuthor Commented:
Unfortunately, i get the follow errors:

For strtol():

Error      1      error C2664: 'strtol' : cannot convert parameter 1 from 'CString' to 'const char *'      
Error      2      error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *'



For_tcstol():

Error      3      error C2664: 'wcstol' : cannot convert parameter 2 from 'char **__w64 ' to 'wchar_t **'
Error      4      error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *'
0
jkrCommented:
Ooops, sorry...
#include <stdlib.h>

//...

CString HexStrToDecStr(const CString& sHex) {

  CString sDec;
  char *p;
  sDec.Format("%d",_tcstol((TCHAR*)sHex,&p,16));

  return sDec;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mr_stevieAuthor Commented:
Unfortunately, that still gives me problems.

However, I think I've managed to figure it out.

For some reason, char* isn't working properly and I'm using a wchar_t*.

Also,  "%d" needs to be _T("%d") to convert to UNICODE.

Otherwise, the following code seems works fine!

Thank you very much!
wchar_t *p;
sDec.Format( _T("%d") , _tcstol( sHex, &p, 16 ) );

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.