How to define a new number system in C ++

Essentially, I'm trying to create a base system with numbers 62 in C ++ (an alphanumeric number system including az, AZ, and 0-9). How would this be done? I tried using a char array as follows:

const char alphaNum[62] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', ' y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

however, write functions to use this array and try to count in order to use too much code to be practical (from 0 to 61, of course, just select it from the array. The problem arises when you try to make multi-valued numbers, i.e. 00) . It would be much easier to say foobar++;. Does anyone have a way to define number systems, or at least a way to do it so that I don't have to write a case for every time it reaches Z?

EDIT: it should have been const char, I don’t know why VS decided it would be interesting not to copy it.

+4
source share
3 answers

The following may help: ( http://ideone.com/y1gZDF ) (You can change the internal representation to suit your needs, like BigNumber).

class N62
{
public:
    explicit N62(const std::string& digits) : N62(digits.c_str(), digits.size()) {}
    N62(const char* digits, std::size_t len) : value(0u)
    {
        for (std::size_t i = 0; i != len; ++i) {
            auto pos = std::find(std::begin(base), std::end(base), digits[i]);
            if (pos == std::end(base)) {
                throw std::runtime_error("incorrect digit");
            }
            value *= 62;
            value += pos - std::begin(base);
        }
    }
    N62(std::size_t value) : value(value) {}
    operator std::size_t () const { return value; }
    std::string str() const
    {
        if (value == 0u) {
            return "0";
        }
        std::string res;
        for (std::size_t n = value; n != 0; n /= 62) {
            res.push_back(base[n % 62]);
        }
        std::reverse(res.begin(), res.end());
        return res;
    }

private:
    std::size_t value;
private:
    static constexpr char base[] =
        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
};

Alternatively, you can add a user line literal as follows:

N62 operator "" _n62 (const char *t, std::size_t len)
{
    return N62(t, len);
}

And use it this way: "1Z"_n62(which resolves 123in decimal).

+4
source

You need to separate the external (to User) representation and the internal representation.


. , ; .

.


. ( const.) . , 16 16, , . - . , .

, 17 18. 62 #define const integer.

1:
. , char 0 255, 62.

std::vector<unsigned char> . , .

:

  add 1 to digit.
  if digit value > 62
  {
     set digit to zero.
     Load digit with next greater column value (i.e. vector[position + 1];
     Repeat at top of algorithm
  }

, (10, 8, 16 ..).
, , . ().

, .

+8

. , . , :

base62 ?

, base62. "0x" "$" base16, "0" , "%" , "*" , , - , : ~ Uu3n

, base62 ( , ) .

base62?

. base62.

 printf("%d in base62 is %D \n",value,value);

shoud

 printf("%d in base62 is %s \n",value,tob64(value));

.


, - (, Integer Java).

+1
source

Source: https://habr.com/ru/post/1544604/


All Articles