C++中的数字系统

C++中的数字系统,c++,C++,我有一个程序,应该翻译的价值观​​从一个数字系统到另一个数字系统,但我有一个问题,_itoa_s写道,[错误]“_itoa_s”未在此范围内声明我尝试连接库,我也尝试用snprintf替换itoa,但这对编译器没有帮助。还有更多错误,请帮助我修复错误, 代码如下: #include <cstring> #include <iostream> #include <cmath> using namespace std; int main() { setloca

我有一个程序,应该翻译的价值观​​从一个数字系统到另一个数字系统,但我有一个问题,_itoa_s写道,[错误]“_itoa_s”未在此范围内声明我尝试连接库,我也尝试用snprintf替换itoa,但这对编译器没有帮助。还有更多错误,请帮助我修复错误, 代码如下:

#include <cstring>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
setlocale(LC_ALL, "rus");
int in, iz, k, s = 0, p;
char str[255];
cout << "Enter the number system from which you want to translate" << endl;
cin >> iz;
cout << "Enter the number system to which we will translate" << endl;
cin >> in;
cout << "Enter the number" << endl;
cin >> str;
p = strlen(str) - 1;
for (int i = 0; i < str[i]; i++)
{
    switch (str[i])
    {
    case 'a': k = 10; break;
    case 'b': k = 11; break;
    case 'c': k = 12; break;
    case 'd': k = 13; break;
    case 'e': k = 14; break;
    case 'f': k = 15; break;
    case '1': k = 1; break;
    case '2': k = 2; break;
    case '3': k = 3; break;
    case '4': k = 4; break;
    case '5': k = 5; break;
    case '6': k = 6; break;
    case '7': k = 7; break;
    case '8': k = 8; break;
    case '9': k = 9; break;
    case '0': k = 0; break;
    }
    s = s + k * pow(iz, p);
    p--;
}
char result[255];
_itoa_s(s, result, in);
cout << "The result of a translation from a radix  " << iz << " to radix " << in << " = " << result;

return 0;
}

这里有一个不涉及交换机、pow或itoa的替代方案:


我不明白你为什么需要itoa?无论如何,iToaais是编译器特定的变体,它不能从C++标准中获得。ῥεῖ 好的,我可以用什么来代替itoa?我了解到它不适用于传统的编译器,并且使用了snprintf等替代方法,但它不起作用。这里仍然有没有长度限制的版本,您只需要确保缓冲区足够大,可以接收结果。使用sprintf,您可以提前进行测试。不过,C++标准和最简单的方法是使用STD::OSTRIGNSWATE和STD:String来进行结果。是否需要使用Switter语句?这可以简化为使用数组;对于字母数字,字符c=值-10+a;注意:这假定字母数字采用ASCII编码。对不起,EBCDIC的粉丝们。
// Notice, the index is the value of the digit.
const std::string digits[] = "0123456789abcdef";
//...
const size_t length = str.len;
// Note: iz is the radix for "input" conversion
int number = 0;
for (unsigned int i = 0; i < length; ++i)
{
  const std::string::size_type position = digits.find(str[i]);
  number = number * iz; // Shift the number by one digit.
  number += position;
}
std::string translated_number;
while (number > 0)
{
  const unsigned int index = number % output_radix; // Output_radix == 'in'
  const char digit_character = digits[index];
  translated_number.insert(0, 1, digit_character);
  number = number / output_radix;
}