Binary

I have been working on a system to to covert a string into ones and zeros. I have been doing a lot of looking about and made many attempts to make everything work. My effort have largely been in vain if anyone has ideas any help would be nice.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <cstddef>
#include <vector>
#include <windows.h>
#include <mmsystem.h>
#include <map>
#include <stdio.h>
#include <bitset>
#include <stdint.h>
using namespace std;

uint32_t toBinary (string myString)
{
    uint32_t outValue;
    //string myString = inData;
    for (std::size_t i = 0; i < myString.size(); ++i)
    {
        bitset<8> b(myString.c_str()[i]);
        //outValue = b.to_ulong();
    }
    return outValue;
}

int main ()
{
    cout << toBinary("Test Nigger") << endl;
    system("color 9");
}
Don't know if this is what you are seeking:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <bitset>
#include <limits>

using byte = unsigned char ;
constexpr std::size_t BITS_PER_BYTE = std::numeric_limits<byte>::digits ;

// return the bits in a byte as a string of zeroes and ones
std::string to_bits( byte b )
{ return std::bitset<BITS_PER_BYTE>(b).to_string() ; }

std::string to_bits( const std::string& text )
{
    std::string result ;
    for( byte b : text ) result += to_bits(b) ;
    return result ;
}

int main()
{
    const std::string  bits = to_bits( "Atton (5)" ) ;
    for( std::size_t i = 0 ; i < bits.size() ; i += BITS_PER_BYTE )
        std::cout << bits.substr( i, BITS_PER_BYTE ) << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/a6ab5fbe584c7358
That code seems to have quite a few error in it.
C++98:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <bitset>
#include <limits>

// using byte = unsigned char ;
// constexpr std::size_t BITS_PER_BYTE = std::numeric_limits<byte>::digits ;

typedef unsigned char byte ;
const std::size_t BITS_PER_BYTE = std::numeric_limits<byte>::digits ;

// return the bits in a byte as a string of zeroes and ones
std::string to_bits( byte b )
{ return std::bitset<BITS_PER_BYTE>(b).to_string() ; }

std::string to_bits( const std::string& text )
{
    std::string result ;
    
    // for( byte b : text ) result += to_bits(b) ;
    for( std::size_t i = 0 ; i < text.size() ; ++i ) result += to_bits( text[i] ) ;
    
    return result ;
}

int main()
{
    const std::string  bits = to_bits( "Atton (5)" ) ;
    for( std::size_t i = 0 ; i < bits.size() ; i += BITS_PER_BYTE )
        std::cout << bits.substr( i, BITS_PER_BYTE ) << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/f2f867feefa08ac9
Hmm... Digital Mars, is it? They don't have a standard library of their own and are still flogging an outdated version of STLport.

Try
1
2
// return std::bitset<BITS_PER_BYTE>(b).to_string() ;
return std::bitset<BITS_PER_BYTE>(b).to_string<std::string>() ;

It may work.


Or better, switch to another compiler. Several free alternatives are available for windows:

Visual Studio Express 2013 is pretty good; decent compiler with an excellent library.
http://www.microsoft.com/en-us/download/details.aspx?id=43733

There is also the LLVM clang++ compiler + Microsoft library option:
http://llvm.org/builds/

The GNU offering too is adequate (the library is poor, but still a whole lot better than what Digital Mars has to offer):
http://nuwen.net/mingw.html
Might change that now.
Strange but how could you store the end result to a variable.
Thanks for your help I am going to try to put this into service.
Also does this work in revere.
Last edited on
Topic archived. No new replies allowed.