Converting an int into a base 2 cstring/string

£可爱£侵袭症+ 提交于 2019-12-23 16:34:28

问题


In visual studio on my PC I can use itoa() to convert from a base ten int to a base 2 c-string. However when I am on a Linux machine that function isn't supported. Is there another quick way of doing this kind of conversion? I know how to use a string stream and I can use the dividing and modding to convert to another base manually.

I was just hopping that there was an easier way of accessing the binary representation of an int.


回答1:


You could use std::bitset<N> with a suitable N (e.g., std::numeric_limits<int>::digits):

std::string bits = std::bitset<10>(value).to_string();

Note that ints just represent a value. They are certainly not base 10 although this is the default base used when formatting them (which can be easily change to octal or hexadecimal using std::oct and std::hex). If anything, ints are actually represented using base 2.




回答2:


Same in C:

short UtilLitoa(long value, char* str, short base);
void UtilStrReverse(char* begin, char* end);
static const char c36Digits[] =  "0123456789abcdefghijklmnopqrstuvwxyz";

short UtilLitoa(long value, char* str, short base)
 {  
  char* wstr=str;
  int sign;
  unsigned long res;
  /* Validate base */
  if (base<2 || base>36)
   {
    if(str!=NULL)
     *str='\0';
    return(0);
   }
  /* Take care of sign */
  if ((sign=value) < 0)
   value = -value;
  res=value;
  /* Conversion. Number is reversed */
  do
   {
    value=(long)res;
    res=res/base;
    if(str!=NULL)
     *wstr = c36Digits[(unsigned long)value-res*base];
    wstr++;
   }
  while(res);
  if(sign<0)
   *wstr++='-';
  *wstr='\0';
  /* Reverse string */
  UtilStrReverse(str, wstr-1);
  return(wstr-str);
 }

void UtilStrReverse(char* begin, char* end)
{
  char aux;

  if(begin==NULL)
   return;
  if(end==NULL)
   end=begin+strlen(begin)-1;
  while(end>begin)
   {
    aux=*end;
    *end--=*begin;
    *begin++=aux;
   }    
}



回答3:


The most straightforward solution for the C++ string:

std::string to_bin(unsigned int value) {
  if (value == 0) return "0";
  std::string result;
  while (value != 0) {
    result += '0' + (value & 1);
    value >>= 1;
  }
  std::reverse(result.begin(), result.end());
  return result;
}

For different bases (2 <= base <= 36):

std::string to_base(unsigned int value, int base) {
  if (value == 0) return "0";
  std::string result;
  while (value != 0) {
    int digit = value % base;
    result += (digit > 9 ? 'A' + digit - 10 : digit +'0');
    value /= base;
  }
  std::reverse(result.begin(), result.end());
  return s;
}

edit: fixed for negative numbers by changing argument from int to unsigned int




回答4:


Yet another version

#include <string>
#include <limits>
#include <iostream>

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

template <class Number> void toBinaryRepresentation(
    const Number& n,
    std::string& str,
    const bool reverse = false)
{
    std::size_t i,j; // iterators
    const std::size_t numbits = std::numeric_limits<unsigned char>::digits;
    const char *raw = reinterpret_cast<const char*>(&n);
    str.resize(sizeof(n) * numbits);
    for (i = 0; i != sizeof(n); ++i)
    {
        for (j = 0; j < numbits; ++j)
        {
            str[i * numbits + j] = ((raw[i] >> j) & 1) + 48;
        }
    }
    if (reverse == false) std::reverse(str.begin(), str.end());
}

int main(int argc, char *argv[]) {
    if (argc != 3)
    {
        std::cerr << "Usage: " << argv[0];
        std::cerr << " [int|long|float] [number]" << std::endl;
        return -1;
    }
    #define test_start(type, from_string_to_type)          \
        if (std::strcmp(argv[1], TOSTRING(type)) == 0) {   \
            const type num = from_string_to_type(argv[2]); \
            toBinaryRepresentation(num, binary);           \
            std::cout << binary << std::endl;              \
        }

    #define test(type, from_string_to_type)                   \
        else if (std::strcmp(argv[1], TOSTRING(type)) == 0) { \
            const type num = from_string_to_type(argv[2]);    \
            toBinaryRepresentation(num, binary);              \
            std::cout << binary << std::endl;                 \
        }
    std::string binary;
    test_start(int, std::atoi)
    test(long, std::atol)
    test(float, std::atof)
    #undef test_start
    #undef test
    return 0;
}


来源:https://stackoverflow.com/questions/17632775/converting-an-int-into-a-base-2-cstring-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!