9

I have byte to binary string function,

std::string byte_to_binary(unsigned char byte)
{
    int x = 128;
    std::ostringstream oss;
    oss << ((byte & 255) != 0);

    for (int i = 0; i < 7; i++, x/=2)
       oss << ((byte & x) != 0);

    return oss.str();
}

How can i write an int to bits in same way? I don't want extra 0's at the beginning of binary string so that is why i can't figure out how to create a variable length each time. Also, i'm not using std::bitset.

9
  • 5
    Well, then the question is, why are you not using std::bitset? Commented May 18, 2011 at 0:44
  • Because I'm using bitwise-operators. Commented May 18, 2011 at 0:50
  • 4
    @parc: And bitset overloads them. Next reason? Commented May 18, 2011 at 0:52
  • 8
    @parc: You're using ostringstream and string, so you're clearly coding in C++. Use C++ classes for the stuff they're made for. Next reason? :P This may sound a bit harsh, but you'll be thankful later on. Commented May 18, 2011 at 1:08
  • I'll convert them to use char arrays later. Commented May 18, 2011 at 1:09

4 Answers 4

18

I'll just post this as an answer. It is shorter, safer and, what's most important, it is done.

#include <string>
#include <bitset>
#include <type_traits>

// SFINAE for safety. Sue me for putting it in a macro for brevity on the function
#define IS_INTEGRAL(T) typename std::enable_if< std::is_integral<T>::value >::type* = 0

template<class T>
std::string integral_to_binary_string(T byte, IS_INTEGRAL(T))
{
    std::bitset<sizeof(T) * CHAR_BIT> bs(byte);
    return bs.to_string();
}

int main(){
    unsigned char byte = 0x03; // 0000 0011
    std::cout << integral_to_binary_string(byte);
    std::cin.get();
}

Output:

00000011

Changed function name, though I'm not happy with that one... anyone got a nice idea?

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks, btw did you get that output from big endian or little endian OS?
+1 if you generalize that into a function template using sizeof(T) as the template argument to std::bitset<> so that it works with other integral types.
@ildjarn: Done, and thanks to you I noticed a bug.. one byte is CHAR_BIT bit, not 4 bit. :)
@parc: Little-endian, like most OS on x86-64. Take a look at Wikipedia for a listing
Oh, and I also added some SFINAE for safety, within a macro for brevity. Sue me for it. :)
|
5

Something like this should work (though I hacked it up quickly and haven't tested):

#include <string>
#include <climits>

template<typename T>
std::string to_binary(T val)
{
  std::size_t sz = sizeof(val)*CHAR_BIT;
  std::string ret(sz, ' ');
  while( sz-- )
  {
    ret[sz] = '0'+(val&1);
    val >>= 1;
  }
  return ret;
}

1 Comment

Portable code should use CHAR_BIT instead of hardcoding 8 and hold the result in std::size_t instead of int.
5

You can do it using std:bitset and convert any number into bit string of any size, for example 64

#include <string>
#include <iostream>
#include <bitset>
using namespace std;

int main() {

 std::bitset<64> b(836); //convent number into bit array
 std::cout << "836 in binary is " <<  b << std::endl;

 //make it string
 string mystring = b.to_string<char,char_traits<char>,allocator<char> >();
 std::cout << "binary as string " << mystring << endl;
}

Comments

1

Since you mentioned your wish for C style in the comments, you might consider using itoa (or _itoa) if you are not worried about ANSI-C standard. Many compilers support it in stdlib.h. It also strips the leading 0's:

unsigned char yourGoldenNumber = 42;
char binCode[64];
itoa(yourGoldenNumber,binCode,2); // third parameter is the radix
puts(binCode); // 101010

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.