Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

dword to string

Posted on 2003-12-01
4
Medium Priority
?
1,957 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 200 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

886 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