为什么转换不起作用?
Why is conversion not working?
我想开发一种能够在 C++ 中进行 AnyBase 2 AnyBase 转换 的算法。
所以我开始只是翻译 this javascript code into C++ and ended with the use of a BigInt - library 因为它当然不能使用整数(long int、long double 等)精度 - 所以我不得不使用 BigInt 库。
这是我想出的代码。直到现在我还没有收到错误消息或警告,但转换似乎没有正确完成它的工作:
string enc1 = convertBaseBigInt("A", 64, 4);
cout << "enc1: " << enc1 << endl; // gets "210000"
string dec1 = convertBaseBigInt(enc1, 4, 64); // gets "2g0" (instead of "A")
cout << "dec1: " << dec1 << endl;
请看我的代码:
std::string convertBase(string value, int from_base, int to_base) {
string range = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/";
string from_range = range.substr(0, from_base),
to_range = range.substr(0, to_base);
int dec_value = 0;
int index = 0;
string reversed(value.rbegin(), value.rend());
for(std::string::iterator it = reversed.begin(); it != reversed.end(); ++it) {
index++;
char digit = *it;
if (!range.find(digit)) return "error";
dec_value += from_range.find(digit) * pow(from_base, index);
}
string new_value = "";
while (dec_value > 0) {
new_value = to_range[dec_value % to_base] + new_value;
dec_value = (dec_value - (dec_value % to_base)) / to_base;
}
return new_value;
}
我希望有人能帮我找到我的错误,因为我自己似乎找不到它。
预先感谢一百万,Tempi。
我认为你的问题是你没有使用正确的 'index' 值,它应该从 0 开始而不是 1
移除
index++
并修改行
BigInt::Rossi add(to_string((int)(from_range.find(digit)
* pow(from_base, index))), BigInt::DEC_DIGIT);
到
BigInt::Rossi add(to_string((int)(from_range.find(digit)
* pow(from_base, index++))), BigInt::DEC_DIGIT);
我还建议删除运算符关键字
例如
decValue.operator>(BigInt::Rossi("0", BigInt::DEC_DIGIT))
当这更清楚时
decValue > BigInt::Rossi("0", BigInt::DEC_DIGIT)
我想开发一种能够在 C++ 中进行 AnyBase 2 AnyBase 转换 的算法。
所以我开始只是翻译 this javascript code into C++ and ended with the use of a BigInt - library 因为它当然不能使用整数(long int、long double 等)精度 - 所以我不得不使用 BigInt 库。
这是我想出的代码。直到现在我还没有收到错误消息或警告,但转换似乎没有正确完成它的工作:
string enc1 = convertBaseBigInt("A", 64, 4);
cout << "enc1: " << enc1 << endl; // gets "210000"
string dec1 = convertBaseBigInt(enc1, 4, 64); // gets "2g0" (instead of "A")
cout << "dec1: " << dec1 << endl;
请看我的代码:
std::string convertBase(string value, int from_base, int to_base) {
string range = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/";
string from_range = range.substr(0, from_base),
to_range = range.substr(0, to_base);
int dec_value = 0;
int index = 0;
string reversed(value.rbegin(), value.rend());
for(std::string::iterator it = reversed.begin(); it != reversed.end(); ++it) {
index++;
char digit = *it;
if (!range.find(digit)) return "error";
dec_value += from_range.find(digit) * pow(from_base, index);
}
string new_value = "";
while (dec_value > 0) {
new_value = to_range[dec_value % to_base] + new_value;
dec_value = (dec_value - (dec_value % to_base)) / to_base;
}
return new_value;
}
我希望有人能帮我找到我的错误,因为我自己似乎找不到它。
预先感谢一百万,Tempi。
我认为你的问题是你没有使用正确的 'index' 值,它应该从 0 开始而不是 1
移除
index++
并修改行
BigInt::Rossi add(to_string((int)(from_range.find(digit)
* pow(from_base, index))), BigInt::DEC_DIGIT);
到
BigInt::Rossi add(to_string((int)(from_range.find(digit)
* pow(from_base, index++))), BigInt::DEC_DIGIT);
我还建议删除运算符关键字
例如
decValue.operator>(BigInt::Rossi("0", BigInt::DEC_DIGIT))
当这更清楚时
decValue > BigInt::Rossi("0", BigInt::DEC_DIGIT)