Solved

Convert a CString to an int.

Posted on 2000-03-23
9
308 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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