Solved

How to change DWORD to a string?

Posted on 2001-07-13
16
362 Views
Last Modified: 2012-06-27
In my program (using MS VC++) I want to print avariable of type DWORD. But I have to change it to a type of char [] or a string. How?
0
Comment
Question by:chenwei
[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
  • 6
  • 4
  • 3
  • +3
16 Comments
 

Author Comment

by:chenwei
ID: 6279801
And the value of this variable is not a number but something like this: SERVICE_RUNNING or SERVICE_STOPPED.
0
 

Expert Comment

by:pigsmayfly
ID: 6279862
DWORD means "double word" which is type of "int".  In Visual C++, this is really an "unsigned long".

If, of course the DWORD is really a number then there is no sense changing it to a string of any kind.  So

DWORD i = 10;
std::cout << i << std::endl;

print number 10

If DWORD is a pointer to a char array, then you should "change" it by typecasting, depending on what it originally is, ie:

char s[100] = "Hello";
DWORD addr = (DWORD)&s;
std::cout << (char *)addr << std::endl;

If you know that DWORD is a pointer but do not know what it originally was, then you are in trouble.
0
 
LVL 6

Expert Comment

by:MichaelS
ID: 6279912
_ltoa() function could help
0
Independent Software Vendors: 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!

 

Expert Comment

by:barzangy
ID: 6280093
Use string streams like this:

#include <windows.h>
#include <iostream>
#include <string>
#include <strstream>

using namespace std;

int main() {

     DWORD a = 1030;
     ostrstream s1 = ostrstream();    
     s1 << " " << a << " ";
     string DWORDSTR = s1.str();
     DWORDSTR.erase(DWORDSTR.begin() + 6, DWORDSTR.end());
     cout << DWORDSTR << endl;
     cin.ignore();
     return 0;
}
0
 

Author Comment

by:chenwei
ID: 6280106
And the value of this variable is not a number but something like this: SERVICE_RUNNING or SERVICE_STOPPED.
0
 

Author Comment

by:chenwei
ID: 6280133
Thanks for the help. But maybe there is misunderstanding. Surely I know how to change an integer to a string and I know DWORD is an integer as well. But what I want is how to change the SERVICE_RUNNING to a string.

In my program there is a function like:
...
...
DWORD m_dwState;
...
BOOL b = QueryServiceStatus(hService,&ss);
m_dwState = ss.dwCurrentState;
...
if (m_dwState == SERVICE_RUNNING)
...
...

SERVICE_RUNNING is an integer but I don't know huw much it is, so I want to change it to a string like "SERVICE_RUNNING".

Maybe it's impossible?
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 6280143
I think maybe you are talking about some kind of enumeration, the simplest thing is to use a switch statement:

DWORD value;
std::string message;

switch (value)
{
case 1:
   message = "SERVICE_RUNNING";
   break;
case 2:
   message = "SERVICE_STOPPED";
   break;
default:
   message = "Unknown Code!";
}

a better, but possibly more complicated solution might be to use a map to store the strings.

0
 

Expert Comment

by:barzangy
ID: 6280201
You can use some macro's with #:

#include <iostream>
using namespace std;

#define ERR_OK
#define ERR_1
#define ERR_2
#define ERR_3
#define ERR_4

#define CASE(anerror) case(anerror) : cout << "The error " << #anerror << " occured \n"; break;

void out_error (int err) {
 switch(err) {
  CASE(ERR_OK)
  CASE(ERR_1)
  CASE(ERR_2)
  CASE(ERR_3)
  CASE(ERR_4)
 }

int main () {
  out_error(3);
  return 0;
}

0
 

Expert Comment

by:barzangy
ID: 6280205
Oops, The definitions above should be:

#define ERR_OK 0
#define ERR_1  1
#define ERR_2  2
#define ERR_3  3
#define ERR_4  4
0
 

Expert Comment

by:barzangy
ID: 6280216
I also forgot a "}" in the end of the function out_error!!!
0
 
LVL 22

Expert Comment

by:nietod
ID: 6280287
>> You can use some macro's with #:
You can also smoke cigarettes while taking a bath in gasoline.   Personally I wouldn't

In C++ you should never use #define to create constants.  C++ supports true constants that are far far safer and more logical that #defines

As a variation in JasonClarke's idea, if the constants are numerically sequential (or reasonably so), you can skip the case statement and use an array.   I beleive you have that case here.  L:ike

const char *ServiceList[] = {"SERVICE_STOPPED",
                                             "SERVICE_START_PENDING",
                                             "SERVICE_STOP_PENDING",
                                             "SERVICE_RUNNING",
                                             "SERVICE_CONTINUE_PENDING"};

Then you can use somethine like

string S = ServiceList[TheServiceDWORDValue];
0
 

Author Comment

by:chenwei
ID: 6280510
Thankd for the information from all sites. I know the methods you told me. But the SERVICE_STOPPED and SERVICE_RUNNING are predefined in VC++.

I know now it's impossible to do what I ask. I should get out how the SERVICE_STOPPED and SERVICE_RUNNING are defined in MS VC++.
0
 
LVL 22

Accepted Solution

by:
nietod earned 20 total points
ID: 6280541
>> I know now it's impossible to do what I ask.
We conviced you of that using several examples that did it?

>> I should get out how the SERVICE_STOPPED
>> and SERVICE_RUNNING are defined in MS VC++.

They are pre-processor definitions (#defines).  they are defined in winsvc.h
0
 

Author Comment

by:chenwei
ID: 6280602
Hi nietod,

I got it. Thanks. I want to give you the points but I am not sure if you've locked the question or not. If no, please lock this question so I can give the points.
0
 

Author Comment

by:chenwei
ID: 6280627
Hi nietod,

I got it. Thanks. I want to give you the points but I am not sure if you've locked the question or not. If no, please lock this question so I can give the points.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6280820
The question is currently locked by barzangy.  Notice how his post has the "propsed answer" title.   Only one expert can lock a question at a time so I cannot lock.

Your choices (always)are to either accept that as the answer by grading it, or reject it so other experts can lock the question.  Also, if there is no proposed answer (if one was never proposed, or if you rejected all the proposed answers) you also have the choice of accepting comments as answers.  Each comment will have a link for doing so on the right side of their title.  (Make sure you choose a comment for the right expert, lots of people seem to choose the wrong one by mistake.  Remember the title is above the comment, not below.)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

691 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