Solved

C++ CString Hex to Integer Conversion

Posted on 2007-04-06
7
1,653 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thread-safe code in c++ 2 101
best sources to up-to-date in C++? 8 81
How to print into std::string like sprintf using C++11? 12 60
Precision Problem in C++ 7 32
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

773 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