Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Convert a CString to an int.

Posted on 2000-03-23
Medium Priority
310 Views
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
Question by:Nitro187
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2
• +2

LVL 2

Accepted Solution

paulburns earned 400 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

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

ID: 2652167
And it works for Ur case also.

VinExpert
0

LVL 3

Expert Comment

ID: 2652216
You can just do this:

int val = 0;
CString text = "0x8b";
sscanf(text, "%x", &val);
0

LVL 23

Expert Comment

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

ID: 2652226
CString str(_T("0x8b"));
LPTCSTR lpszStop;

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

Author Comment

ID: 2653448
Thanks a lot paul!
0

Author Comment

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

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

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
###### Suggested Courses
Course of the Month9 days, 14 hours left to enroll