在 C++ 中将无符号整数转换为其八进制表示形式的最佳方法是什么,反之亦然?

What is the best way to convert unsigned integers to their octal representations and vice versa in C++?

目前我正在使用 while 循环:

std::string to_octal(unsigned int num)
{
    int place = 1, remainder, octal = 0;
    while (num != 0)
    {
        remainder = num % 8;
        decimal /= 8;
        octal += remainder * place;
        place *= 10;
    }
    return std::to_string(octal);
}

unsigned int to_num(std::string octal)
{
    unsigned int octal_n = std::stoi(octal);
    int place = 1, remainder, num = 0;
    while (num != 0)
    {
        remainder = octal_n % 10;
        octal_n /= 10;
        num += remainder * place;
        place *= 8;
    }
    return num;
}

这似乎效率低下。有更好的方法吗?

打印不同基数的数字:

#include <iostream> 

int main () {
  int n = 123;
  std::cout << std::dec << n << '\n';
  std::cout << std::hex << n << '\n';
  std::cout << std::oct << n << '\n';
  return 0;
}

没有十进制unsigned int、十六进制unsigned int或八进制unsigned int这样的东西。只有一个unsigned int。仅当您要将该类型的对象打印到终端或文件时才会有所不同。从这个角度来看,函数

unsigned int decimal_to_octal(unsigned int decimal);

完全没有意义。使用是有意义的:

struct decimal_tag {};
struct hexadecimal_tag {};
struct octal_tag {};

// Return a string that represents the number in decimal form
std::string to_string(unsigned int number, decimal_tag);

// Return a string that represents the number in hexadecimal form
std::string to_string(unsigned int number, hexadecimal_tag);

// Return a string that represents the number in octal form
std::string to_string(unsigned int number, octal_tag);

和他们的同行。

// Extract an unsigned number from the string that has decimal representation
unsigned int to_number(std::string const& s, decimal_tag);

// Extract an unsigned number from the string that has hexadecimal representation
unsigned int to_number(std::string const& s, hexadecimal_tag);

// Extract an unsigned number from the string that has octal representation
unsigned int to_number(std::string const& s, octal_tag);

演示程序如下:

#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>

struct decimal_tag {};
struct hexadecimal_tag {};
struct octal_tag {};

// Return a string that represents the number in decimal form
std::string to_string(unsigned int number, decimal_tag)
{
   std::ostringstream str;
   str << std::dec << number;
   return str.str();
}

// Return a string that represents the number in hexadecimal form
std::string to_string(unsigned int number, hexadecimal_tag)
{
   std::ostringstream str;
   str << std::hex << number;
   return str.str();
}

// Return a string that represents the number in octal form
std::string to_string(unsigned int number, octal_tag)
{
   std::ostringstream str;
   str << std::oct << number;
   return str.str();
}

// Extract an unsigned number from the string that has decimal representation
unsigned int to_number(std::string const& s, decimal_tag)
{
   std::istringstream str(s);
   unsigned int number;
   str >> std::dec >> number;
   return number;
}

// Extract an unsigned number from the string that has hexadecimal representation
unsigned int to_number(std::string const& s, hexadecimal_tag)
{
   std::istringstream str(s);
   unsigned int number;
   str >> std::hex >> number;
   return number;
}

// Extract an unsigned number from the string that has octal representation
unsigned int to_number(std::string const& s, octal_tag)
{
   std::istringstream str(s);
   unsigned int number;
   str >> std::oct >> number;
   return number;
}

int main()
{
   unsigned int n = 200;
   std::cout << "200 in decimal: " << to_string(n, decimal_tag()) << std::endl;
   std::cout << "200 in hexadecimal: " << to_string(n, hexadecimal_tag()) << std::endl;
   std::cout << "200 in octal: " << to_string(n, octal_tag()) << std::endl;

   std::cout << "Number from decimal form (200): " << to_number("200", decimal_tag()) << std::endl;
   std::cout << "Number from hexadcimal form (c8): " << to_number("c8", hexadecimal_tag()) << std::endl;
   std::cout << "Number from octal form (310): " << to_number("310", octal_tag()) << std::endl;
}

及其输出:

200 in decimal: 200
200 in hexadecimal: c8
200 in octal: 310
Number from decimal form (200): 200
Number from hexadcimal form (c8): 200
Number from octal form (310): 200

当然,旧的 C 方法在 C++ 中仍然有效:使用 %o Format specifier.

printf("%o", n);

如果你想在字符串中使用 sprintf(好的,这意味着你必须注意内存分配来存储结果,与 std::oct 相比这是一个缺点)。