#include <string>
#include <algorithm>
#include <iostream>
#include <climits>
#include <limits>


std::string tostr(unsigned long x)
{
  // this will mask out the lower "byte" of the number
  const unsigned char mask = std::numeric_limits<unsigned char>::max();
  
  if (x == 0)
    return std::string("0");
    
  std::string res;
  for (; x > 0; x >>= CHAR_BIT) {
    res += (char) (x & mask);
  }
  
  std::reverse(res.begin(), res.end());
  return res;
}


int main()
{
  const size_t MAX = sizeof(unsigned long);

  std::string s("abcd");
  // std::string s("\226\226\226\226");
  unsigned long res = 0;

  for (size_t i=0; i < std::min(MAX, s.size()); ++i)
  {
    res <<= CHAR_BIT;
    res += (unsigned char) s[i];
  }
  
  std::cout << std::hex << res << std::endl;
  
  std::cout << tostr(res) << std::endl;
}
