Solved

C++ CString Hex to Integer Conversion

Posted on 2007-04-06
7
1,661 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 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
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 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Least Squares Curve Fitting 4 117
Error C2678: binary '!=': no operator found... 4 65
Error creating a new C++ project in ,net 20 51
How do i run a c++ file? 15 52
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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