Solved

C++ CString Hex to Integer Conversion

Posted on 2007-04-06
7
1,643 Views
Last Modified: 2008-01-09
I have a CString.  I'm using Visual Studio C++ 6.0.  I need to extract a hex value from the string and convert it to an integer..

Here's an example:

CString cstr = "0xffff";

I need that cstr to then be an integer value so I can turn around and do something with the integer value (such as a computation.)
0
Comment
Question by:lcor
7 Comments
 
LVL 16

Accepted Solution

by:
AlexNek earned 125 total points
ID: 18865448
0
 
LVL 19

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 125 total points
ID: 18867584
long l;
l = strtol((char *)cstr,NULL,16);
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 18867587
for more information on strtol please see:
 http://www.thinkage.ca/english/gcos/expl/c/lib/strtol.html
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 18867645
Here is the code for you:
http://www.elecdesign.com/Articles/Index.cfm?AD=1&ArticleID=1779
I hope this is what you need. Only thing is that you need to input the hex value as 'ffff' not 'Oxffff'
You could ommit this ox if it is there in the CString easily by using member functions find,left or right etc.
Best Regards,
DeepuAbrahamK




0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 18867663
long l;
CString cstr = "0xFFFF";
l = strtol((char *)cstr,NULL,0);
0
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 125 total points
ID: 18868750
Sorry, I thought I have provided the link of the sample:
Here we go: http://www.elecdesign.com/Articles/ArticleID/1779/1779.html
Hope this is what you want.
Best Regards,
DeepuAbrahamK
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 125 total points
ID: 18870030
Here's the IOStreams approach:

--------8<--------
#define WINVER 0x500
#include "afxwin.h"
#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
      long l;
      CString cstr = "0xFFFF";
      std::istringstream istr(static_cast<LPCTSTR>(cstr));
      istr >> std::hex >> std::showbase >> l;
      std::cout << l << " in hex is " << std::hex << std::showbase << l << '\n';
}
--------8<--------
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

759 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

23 Experts available now in Live!

Get 1:1 Help Now