Solved

dword to string

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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