Converting a vector of numbers from base to base

How can I convert vector<int>from one database ato vector<int>another bwithout using a library like gmp?
Contain digits of numbers. aand bless than 1024. amay be less or more than b.
I was thinking about using the standard basic conversion algorithm, but the numbers won't even match in long long.

+3
source share
2 answers

I was thinking about using the standard basic conversion algorithm, but the numbers won't even match in long long.

( : "" ) . , , , , (, , ), .

( ) - long division.

0

, , div/mod b a, , . b < a, - , . b > a, ; a ^ k, a ^ k >= b int ( ), b.

, a == b ^ k ^ k == b ( ), , . , "a" "b" , , .

template<int A, int B> int divmod(std::vector<int> &a) {
    // a is a vector of digits in base A
    // divide a by B in place, returning the remainder
    // implementation left as an exercise for the reader
}

template<int A, int B> std::vector<int> cvtBase(std::vector<int> a) {
    // a is a vector of digits in base A
    // convert it to a vector of digits in base B
    // vectors are in little endian order (least significant digit first)
    std::vector<int> b;
    do {
        b.push_back(divmod<A,B>(a));
    } while (!isZero(a));
    return b;
}
+1

All Articles