Solved

dword to string

Posted on 2003-12-01
4
1,890 Views
Last Modified: 2010-07-27
The following gives me a DWORD from a string.

DWORD d = strtoul( "10011001100110011001100110011001", 0, 2 );  

Is there a function out there gives me a string from a DWORD? I want the string (of length 32)to just consist of 1s and zeros.
0
Comment
Question by:staggs28
[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
  • 2
4 Comments
 
LVL 1

Expert Comment

by:travd
ID: 9856104
Try itoa, and make sure your buffer is long enough to handle it:

char buffer[35];
DWORD d = 124324;

itoa( d, buffer, 2 );

Although itoa isn't part of the standard, many environments offer it, including visual studio, AFAIK.
0
 
LVL 1

Expert Comment

by:travd
ID: 9856115
Here ya go - this is what you need:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wcecrt4/html/erlrfusitoacmausitow.asp

It's _itoa, not itoa - and it does what you want (see the example at the bottom with radix = 2).
0
 
LVL 4

Accepted Solution

by:
skypalae earned 50 total points
ID: 9856135
hi,
theres not such function, but it is an easy to write


void dw2bin (unsigned long n, char *s) {
      s[32] = '\0' ;
    for (int i = 31; i >= 0; i--)
            if (n >= (1u << i)) {
                  s[31-i] = '1' ;
                  n -= (1u << i) ;
            } else
                  s[31-i] = '0' ;
}
0
 

Author Comment

by:staggs28
ID: 9857056
Going to accept skypalae's answer - the _atoi function was almost there, but it didn't give me all the leading zeros...

#include <iostream>
#include <string>
#include <windows.h>            
using namespace std;
void dw2bin (unsigned long n, char *s);

void main( void )
{
   char buffer[32];
   string st = "00000000000000010000000000000001";
//   string st = "10000001000000010000000100000001";
       DWORD dw= strtoul( st.c_str(), 0, 2 );

   _itoa( dw, buffer, 2  );
   string st2 = buffer;
   cout << st<<endl;
   cout << st2<<endl;
   cout << dw<<endl;
   cout << buffer<<endl;
   dw2bin(dw, buffer);
   cout << buffer<<endl;
   string st3 = buffer;
   cout << st3<<endl;
}
void dw2bin (unsigned long n, char *s) {
     s[32] = '\0' ;
    for (int i = 31; i >= 0; i--)
          if (n >= (1u << i)) {
               s[31-i] = '1' ;
               n -= (1u << i) ;
          } else
               s[31-i] = '0' ;
}

Gives me the output..


00000000000000010000000000000001
10000000000000001
65537
10000000000000001
00000000000000010000000000000001
00000000000000010000000000000001
Press any key to continue
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

734 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