Solved

Convert a CString to an int.

Posted on 2000-03-23
9
307 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
[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
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

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

int val = 0;
CString text = "0x8b";
sscanf(text, "%x", &val);
0
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 3 195
scoreUp challenge 14 74
how to use laptop or pad camera in vb.net windows application 2 129
"Black Box" Testing of Control System Software 2 125
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

732 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