Convert a DWORD to a binary string?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Convert a DWORD to a binary string?

  1. #1
    Join Date
    Mar 2000
    Location
    Kaysville, UT
    Posts
    228

    Convert a DWORD to a binary string?

    I've got a DWORD I need to convert to a binary string (char array of 1's and 0's). Problem is, I'm not a VC++ programmer, and am stumbling my way through this. Is there an easy way to do this conversion?

    Code:
    #include "stdafx.h"
    #include <windows.h>
    
    int main(int argc, char* argv[])
    {
       DWORD drives;
    
       drives = GetLogicalDrives();
    
       printf("Drives: %d\n", drives);
    
       return 0;
    }

  2. #2
    Join Date
    Jul 2002
    Location
    St. Louis, MO
    Posts
    484
    PHP Code:

    int main
    ()
    {
        
    DWORD dwTemp 0x3AF1;
        
    WORD low LOWORD(dwTemp);
        
    WORD high HIWORD(dwTemp);
        
    char szLow[10];
        
    char szHigh[10];
        
    char szString[32];

        
    WordToBinaryAscii(szLow,low,8);
        
    WordToBinaryAscii(szHigh,high,8);

        
    //this might be the other way around, I forgot
        
    strcpy(szStringszLow);
        
    strcat(szStringszHigh);

        
    //Should print out 0011101011110001
        
    cout<<szString <<endl;

        return 
    0;
    }

    int WordToBinaryAscii(chardstWORD wdint len)
    {
        
    int charOffset 0;
        
    int wdAmt;

        for(
    int x 0lenx++)
            
    dst[x] = '0';

        while((
    len 0) && (wd >= 0))
        {
            
    wdAmt = (int)pow(2, --len);
            
            if(
    wd wdAmt >= 0)
            {
                
    wd -= wdAmt;
                
    dst[charOffset] = '1';
            }
            else
                
    dst[charOffset] = '0';

            
    charOffset++;
        }

        
    dst[charOffset] = 0x00;

        return 
    charOffset;


  3. #3
    Join Date
    Mar 2000
    Location
    Kaysville, UT
    Posts
    228

    Talking Thanks!

    Thank you!

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564
    Another method using bitset ...

    Code:
    #include <windows.h>
    #include <iostream>
    #include <bitset>
    #include <string>
    
    int main()
    {
        DWORD dwValue = 0x3AF1;
    
        std::bitset<32> bits((unsigned long)dwValue);
    
        std::cout << bits << std::endl;
    
        std::string str(bits.to_string());  // convert to std::string()
    
        std::cout << str << std::endl;
    
        return 0;
    }

  5. #5
    Join Date
    Sep 2002
    Location
    California
    Posts
    34
    Don't thank him yet, that was sloppy.

    In Win32, a WORD is 16-bits and a DWORD is 32-bits, so those character buffers need to be larger. The lines where he puts the string together are backwards as well.

    int main()
    {
    unsigned int dwTemp = 0x3AF1;
    WORD low = LOWORD(dwTemp);
    WORD high = HIWORD(dwTemp);
    char szLow[17];
    char szHigh[17];
    char szString[33];

    WordToBinaryAscii(szLow,low,16);
    WordToBinaryAscii(szHigh,high,16);

    //yes, this was the other way around
    strcpy(szString, szHigh);
    strcat(szString, szLow);

    //Should print out 0011101011110001
    cout<< szString <<endl;

    return 0;
    }

  6. #6
    Join Date
    Jul 2002
    Location
    St. Louis, MO
    Posts
    484
    Originally posted by Pug
    Don't thank him yet, that was sloppy.
    It was just an example. Why not contribute for a while before trying to set me on fire.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center