#ifndef morpheo_ToBase2
#define morpheo_ToBase2

/*
 * $Id: ToBase2.h 81 2008-04-15 18:40:01Z rosiere $
 *
 * [ Description ]
 * 
 */

#include <sstream>

namespace morpheo              {

  template<typename T> inline std::string toBase2 (const T& value, const uint32_t & size)
  {
    std::ostringstream res;
    T mask = 1<<(size-1);
    
    while (mask != 0)
      {
	res  << ((value&mask)?'1':'0');
	mask >>= 1;
      }

    return res.str();
  }

  inline std::string toBase2 (const bool    & value)
  {
    return toBase2<bool   >  (value, 1);
  }

  inline std::string toBase2 (const uint8_t & value, const uint32_t & size)
  {
    return toBase2<uint8_t>  (value, size);
  }

  inline std::string toBase2 (const uint16_t& value, const uint32_t & size)
  {
    return toBase2<uint16_t> (value, size);
  }

  inline std::string toBase2 (const uint32_t& value, const uint32_t & size)
  {
    return toBase2<uint32_t> (value, size);
  }

  inline std::string toBase2 (const uint64_t& value, const uint32_t & size)
  {
    return toBase2<uint64_t> (value, size);
  }


}; // end namespace morpheo              

#endif
