?
Solved

C++ CString Hex to Integer Conversion

Posted on 2007-04-06
7
Medium Priority
?
1,672 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
Technology Partners: 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

743 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