Solved

dword to string

Posted on 2003-12-01
4
1,899 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

630 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