Solved

Convert a CString to an int.

Posted on 2000-03-23
9
302 Views
Last Modified: 2013-11-20
You may be thinking "Just use atoi"... but that does not work in this case.  I am trying to convert a CString, "0x8b", to an int value of   0x8b  .    Here is some code that I have used:

char      Txt[3];
int      Val;
Txt = "0x8b";
if (strlen(Txt)==2)
{
      if (Txt[0]>='a') Txt[0]&=~0x20;
      if (Txt[1]>='a') Txt[1]&=~0x20;
      if (((Txt[0]>='0' && Txt[0]<='9') || (Txt[0]>='A' && Txt[0]<='F')) && ((Txt[1]>='0' && Txt[1]<='9') || (Txt[1]>='A' && Txt[1]<='F')))
      __asm {
            mov      ax,word ptr Txt
            sub      ax,0x3030
                  cmp      al,0x11
            setb      dl
            dec      dl
            and      dl,7
            sub      al,dl
                  cmp      ah,0x11
            setb      dl
            dec      dl
            and      dl,7
            sub      ah,dl
                  shl      al,4
            or            al,ah
            movzx      eax,al
            mov      Val,eax
      }
}


This is some code that a friend gave to me in order to do this.  It doesnt work in MFC though.  Is there another way to do this, that works?  Please help!
0
Comment
Question by:Nitro187
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 2

Accepted Solution

by:
paulburns earned 100 total points
Comment Utility
If you are able to use the sscanf function then this will be the easiest way...

int val;
char txt[] = "0x8b";
sscanf(&txt[2], "%x", &val);
0
 
LVL 8

Expert Comment

by:VinExpert
Comment Utility
Hi,

Here is one way to do it.

CString str("0x123");
CString strTemp;
strTemp = str.Right(str.GetLength()-2);
int hexval = 0;
sscanf(strTemp, "%X", &hexval);

Now Ur hexval will contain the value of 0x123.

Try it out.
VinExpert
0
 
LVL 8

Expert Comment

by:VinExpert
Comment Utility
And it works for Ur case also.

VinExpert
0
 
LVL 3

Expert Comment

by:mnewton022700
Comment Utility
You can just do this:

int val = 0;
CString text = "0x8b";
sscanf(text, "%x", &val);
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 23

Expert Comment

by:chensu
Comment Utility
Simply use the C Run-time library function _tcstoul (strtoul/wcstoul).

#include <tchar.h>
#include <stdlib.h>

LPTCSTR lpszStr = _T("0x8b");
LPTCSTR lpszStop;

unsigned long ulNum = _tcstoul(lpszStr, &lpszStop, 16);
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
CString str(_T("0x8b"));
LPTCSTR lpszStop;

long lNum = _tcstol(str, &lpszStop, 16);
0
 

Author Comment

by:Nitro187
Comment Utility
Thanks a lot paul!
0
 

Author Comment

by:Nitro187
Comment Utility
And thanks to everyone else who answered.  As for mnewton,  your answer was the same as his, but he answered first.  Thanks again.
0
 
LVL 3

Expert Comment

by:mnewton022700
Comment Utility
That's fine. The reason I added a comment was to show it going from a CString instead of char *.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now