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

x
?
Solved

C++ CString Hex to Integer Conversion

Posted on 2007-04-06
7
Medium Priority
?
1,684 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
[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
7 Comments
 
LVL 16

Accepted Solution

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

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:Deepu Abraham
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:Deepu Abraham
Deepu Abraham earned 500 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 500 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

610 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