Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将printfxterm代码转换为ostringstream?_C++_C_Terminal - Fatal编程技术网

C++ 如何将printfxterm代码转换为ostringstream?

C++ 如何将printfxterm代码转换为ostringstream?,c++,c,terminal,C++,C,Terminal,问题 我正在尝试将一些Cprintf代码转换为std::ostringstream,但我在十六进制/终端编码方面遇到了问题。我想也许,序列 \x1b(48;5;< /Cord>)需要同时翻译,但我不确定C++中的等价性是什么。 坏结果,部分(C++) 好结果,xterm(C) C代码 void Color_Cube() { for (unsigned green=0; green<6; ++green) { for (unsigned red=0; red<6; +

问题

我正在尝试将一些C
printf
代码转换为
std::ostringstream
,但我在十六进制/终端编码方面遇到了问题。我想也许,序列<代码> \x1b(48;5;< /Cord>)需要同时翻译,但我不确定C++中的等价性是什么。 坏结果,部分(C++)

好结果,xterm(C)

C代码

void Color_Cube()
{
  for (unsigned green=0; green<6; ++green)
  {
    for (unsigned red=0; red<6; ++red)
    {
      for (unsigned blue=0; blue<6; ++blue)
      {
        unsigned color = 16 + (red * 36) + (green * 6) + blue;
        printf( "\x1b[48;5;%dm  ", color );
      }
      printf( "\x1b[0m " );
    }
    printf( "\n" );
  }
}
void Color_Cube_cpp()
{
  std::ostringstream  oss;
  for (unsigned green=0; green<6; ++green)
  {
    for (unsigned red=0; red<6; ++red)
    {
      for (unsigned blue=0; blue<6; ++blue)
      {
        unsigned color = 16 + (red * 36) + (green * 6) + blue;
        oss << std::hex << static_cast<int>(0x1b) << std::dec
          << "[48;5;" << color << "m  ";
      }
      oss << std::hex << static_cast<int>(0x1b) << std::dec << "[0m ";
    }
    oss << "\n";
  }
  std::cerr << oss.str() << "\n";
}
void Color\u Cube()
{

对于(unsigned green=0;green尝试替换
std::hex尝试替换
std::hex您就快到了。您需要流式传输转义字符
'\x1b'
,而不是整数
0x1b

#include <sstream>
#include <iostream>

void Color_Cube_cpp()
{
  std::ostringstream  oss;
  for (unsigned green=0; green<6; ++green)
  {
    for (unsigned red=0; red<6; ++red)
    {
      for (unsigned blue=0; blue<6; ++blue)
      {
        unsigned color = 16 + (red * 36) + (green * 6) + blue;
          oss << "\x1b[48;5;" << color << "m  ";
      }
      oss << "\x1b[0m ";
    }
    oss << "\n";
  }
  std::cerr << oss.str() << "\n";
}
#包括
#包括
void Color_Cube_cpp()
{
std::ostringstream oss;

对于(unsigned green=0;green您就快到了。您需要流式传输转义字符
'\x1b'
,而不是整数
0x1b

#include <sstream>
#include <iostream>

void Color_Cube_cpp()
{
  std::ostringstream  oss;
  for (unsigned green=0; green<6; ++green)
  {
    for (unsigned red=0; red<6; ++red)
    {
      for (unsigned blue=0; blue<6; ++blue)
      {
        unsigned color = 16 + (red * 36) + (green * 6) + blue;
          oss << "\x1b[48;5;" << color << "m  ";
      }
      oss << "\x1b[0m ";
    }
    oss << "\n";
  }
  std::cerr << oss.str() << "\n";
}
#包括
#包括
void Color_Cube_cpp()
{
std::ostringstream oss;
用于(无符号绿色=0;绿色)