[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Converting a Hex CString to Decimal CString

Posted on 2010-03-31
4
Medium Priority
?
2,315 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:mr_stevie
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 29299497
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
 
LVL 1

Author Comment

by:mr_stevie
ID: 29300090
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
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 29301377
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
 
LVL 1

Author Comment

by:mr_stevie
ID: 29302009
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question