0

The main problem I'm having is to read out values in binary in C++ (python had some really quick/easy functions to do this)

I just need the same. So at the moment I have:

ValWord< uint32_t> data1=//[SOME READ FUNCTION]

When I use cout << data1; It gives me a number e.g 2147581953

I want this to be in binary and eventually each "bit" needs to be in its own bin including all '0's e.g:

for (int i = 31; i >= 0; i--) {
      cout << binary[i];
    }  

Would give me this 32 bit long binary number. When I've had it as a straight forwward int, I've used:

    int data[32];
    bitset<32>(N) = data1;

    for(int i=31; i >=0; i--) {
      data[i]=(bitset<32>(N[i]).to_ulong());
    }

    for (int i = 31; i >= 0; i--) {
      cout << data[i];
    }  

But this just gives me error messages. Any ideas?

fiz
  • 906
  • 3
  • 14
  • 38

3 Answers3

3

Maybe this:

#define CPlusPlus11 0

#if CPlusPlus11
int main()
{
    std::uint32_t value(42);
    std::bitset<32> bits(value);
    std::cout << bits.to_string() << std::endl;
    // Storing integral values in the string:
    for(auto i: bits.to_string(char(0), char(1))) {
        std::cout << (int)i;
    }
    std::cout << std::endl;
    return 0;
}
#else
int main()
{
    std::uint32_t value(42);
    std::bitset<32> bits(value);
    std::cout << bits.to_string() << std::endl;
    char data[32];
    for(unsigned i = 0; i < 32; ++i) {
        data[i] = bits[i];
    }
    for(unsigned i = 32; i; --i) {
        std::cout << int(data[i-1]);
    }
    std::cout << std::endl;
    return 0;
}
#endif

Note: Your expressions bitset<32>(N) = data1 and bitset<32>(N[i]) are code smell.

fiz
  • 906
  • 3
  • 14
  • 38
1

In general, transforming a number into a string for a given base, is quite ubiquitous:

#include <cassert>
#include <string>

static char const Digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static size_t const DigitsSize = sizeof(Digits) - 1;

static size_t const BufferSize = 32;

std::string convert(unsigned number, unsigned base) {
    assert(base >= 2 and base <= DigitsSize);

    char buffer[BufferSize] = {};
    char* p = buffer + BufferSize;

    while (number != 0) {
        *(--p) = Digits[number % base];
        number /= base;
    }

    return std::string(p, (buffer + BufferSize) - p);
}

Note: BufferSize was computed for the minimum base of 2, base 1 and base 0 are non-sensical.

Note: if the number can be negative, the simplest is to test for it beforehand, and then use its opposite; a special caveat is that the opposite of the minimum value of a 32 bits integer cannot be represented by a 32 bits integer in base 2.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
0

I have some simple functions i use for this, using stl, here is one for binary:

#include <iostream>
#include <algorithm>

using namespace std;

string binary( unsigned long n )
  {
  string result;

  do result.push_back( '0' + (n & 1) );
  while (n >>= 1);

  reverse( result.begin(), result.end() );
  return result;
  }

int main()
{
    cout << binary(1024) << endl;
   cout << "Hello World" << endl; 

   return 0;
}

Hope this is of use to you!

Let me know if you need something more performant and I can try to rustle up some Assembler code for you.

GMasucci
  • 2,834
  • 22
  • 42
  • Thanks, this gives me the correct binary output. I just now have to figure out how to store each bit into a bin. – fiz Oct 28 '13 at 12:28
  • hi @fiz how do you wish to store them? as a raw binary sequence I take it? (bitfield? binary file? etc.) – GMasucci Oct 28 '13 at 12:30
  • yes e.g 0001 would go into data[0]=1, data[1]=0. data[3]=0 etc. – fiz Oct 28 '13 at 12:31
  • I guess I could have a quick for loop and diving by 10 each time & take the remainder – fiz Oct 28 '13 at 12:32
  • Will need to be in about an hour need to dash home. – GMasucci Oct 28 '13 at 12:51
  • @GMasucci: Note that contrary to the OP's question, your conversion function does not always return 32 bits. If `n` is `0`, it actually returns just `0`. – Matthieu M. Oct 28 '13 at 12:51
  • Someone else has answered it. Thanks for all your advice anyway =) – fiz Oct 28 '13 at 12:57