Solved

dword to string

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now