Solved

Convert a CString to an int.

Posted on 2000-03-23
9
303 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
ID: 2652157
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
ID: 2652159
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
ID: 2652167
And it works for Ur case also.

VinExpert
0
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2652216
You can just do this:

int val = 0;
CString text = "0x8b";
sscanf(text, "%x", &val);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:chensu
ID: 2652217
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
ID: 2652226
CString str(_T("0x8b"));
LPTCSTR lpszStop;

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

Author Comment

by:Nitro187
ID: 2653448
Thanks a lot paul!
0
 

Author Comment

by:Nitro187
ID: 2653455
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
ID: 2662672
That's fine. The reason I added a comment was to show it going from a CString instead of char *.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Separate files macro - Errors on larger data set 18 65
sumHeights  challenge 17 66
How to convert MFC::CString to UTF8 wchar_t* 10 141
mapBully challenge 6 113
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

26 Experts available now in Live!

Get 1:1 Help Now