在任何基数中乘/除以 2 都很简单,因此将任何基数转换为二进制数的最简单方法是重复乘/除以 2,跟踪进位/奇偶校验。
#include <algorithm>
#include <cstdint>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>
// in: a vector representing a bitstring, with most-significant bit first.
// out: a vector representing a tritstring, with least-significant trit first.
static std::vector<uint8_t> b2t(const std::vector<bool>& in) {
std::vector<uint8_t> out;
out.reserve(in.size()); // larger than necessary; will trim later
// for each digit (starting from the most significant bit)
for (bool carry : in) {
// add it to the tritstring (starting from the least significant trit)
for (uint8_t& trit : out) {
// double the tritstring, carrying overflow to higher places
uint8_t new_trit = 2 * trit + carry;
carry = new_trit / 3;
trit = new_trit % 3;
}
if (carry) {
// overflow past the end of the tritstring; add a most-significant trit
out.push_back(1);
}
}
out.reserve(out.size());
return out;
}
// in: a vector representing a tritstring, with most-significant trit first.
// out: a vector representing a bitstring, with least-significant bit first.
static std::vector<bool> t2b(std::vector<uint8_t> in) {
std::vector<bool> out;
out.reserve(2 * in.size()); // larger than necessary; will trim later
bool nonzero;
do {
nonzero = false;
bool parity = false;
for (uint8_t& trit : in) {
// halve the tritstring, starting from the most significant trit
uint8_t new_trit = trit + 3 * parity;
parity = new_trit & 1;
nonzero |= trit = new_trit / 2;
}
// the division ended even/odd; add a most-signiticant bit
out.push_back(parity);
} while (nonzero);
out.reserve(out.size());
return out;
}
int main() {
bool odd = false;
std::string s;
while (std::cin >> s) {
if ((odd = !odd)) {
std::vector<bool> in(s.size());
std::transform(s.begin(), s.end(), in.begin(),
[](char c) {return c - '0';});
std::vector<uint8_t> out(b2t(in));
std::copy(out.rbegin(), out.rend(),
std::ostream_iterator<int>(std::cout));
std::cout << std::endl;
} else {
std::vector<uint8_t> in(s.size());
std::transform(s.begin(), s.end(), in.begin(),
[](char c) {return c - '0';});
std::vector<bool> out(t2b(in));
std::copy(out.rbegin(), out.rend(),
std::ostream_iterator<int>(std::cout));
std::cout << std::endl;
}
}
return 0;
}
$
./a.out
1011
102
102
1011
10001100001101010011010010111000011011101000111101011101000110100101101101111110110011010010111100010110100010101011010100101100001101001000000111011110101001000100011010111011000111101110111001111110110011101011101101001001110010111111100011000110011000111110110111011110110110001111011011011000100101010010111010000110101011010100011010110110000010110111000111000110101000000110000001111110101110010000011000110001010000001001100011000000100100100001100101111000101001001010101101101000011100110001111011110001 的
12010110110220200020211012001000211110222212120220002002120120111221021120100122221020011120010202110111112112110201211201120222000011010100122122121211112101111121002110102112000111200002121211002022100220211220220111010210200222021221020122012102101010100001122200011110210221120122022011202201002002001221211001221112001
12010110110220200020211012001000211110222212120220002002120120111221021120100122221020011120010202110111112112110201211201120222000011010100122122121211112101111121002110102112000111200002121211002022100220211220220111010210200222021221020122012102101010100001122200011110210221120122022011202201002002001221211001221112001 的
10001100001101010011010010111000011011101000111101011101000110100101101101111110110011010010111100010110100010101011010100101100001101001000000111011110101001000100011010111011000111101110111001111110110011101011101101001001110010111111100011000110011000111110110111011110110110001111011011011000100101010010111010000110101011010100011010110110000010110111000111000110101000000110000001111110101110010000011000110001010000001001100011000000100100100001100101111000101001001010101101101000011100110001111011110001
^D
(10112 = 8+2+1 = 11 = 9 + 2 = 1023)
(10001100001101010011010010111000011011101000111101011101000110100101101101111110110011010010111100010110100010101011010100101100001101001000000111011110101001000100011010111011000111101110111001111110110011101011101101001001110010111111100011000110011000111110110111011110110110001111011011011000100101010010111010000110101011010100011010110110000010110111000111000110101000000110000001111110101110010000011000110001010000001001100011000000100100100001100101111000101001001010101101101000011100110001111011110001 2 子> = 7343280200542654154029818354420920722408633707396360612751407162736942742985658428558632312175242897575484682660836397639769592568209070221085927986634481 = 120101101102202000202110120010002111102222121202200020021201201112210211201001222210200111200102021101111121121102012112011202220000110101001221221212111121011111210021101021120001112000021212110020221002202112202201110102102002220212210201220121021010101000011222000111102102211201220220112022010020020012212110012 211120019)