std :: cout将uint8_t作为字符处理 [英] std::cout deal with uint8_t as a character

查看:414
本文介绍了std :: cout将uint8_t作为字符处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我运行以下代码:

std::cout << static_cast<uint8_t>(65);

它将输出:

A

与ASCII等效的数字为65. 这是因为uint8_t被简单地定义为:

Which is the ASCII equivalent of the number 65. This is because uint8_t is simply defined as:

typedef unsigned char uint8_t;

  • 此行为是标准行为吗?

    • Is this behavior a standard?

      不是最好的方法来定义保证以数字而不是字符处理的uint8_t吗?

      Should not be a better way to define uint8_t that guaranteed to be dealt with as a number not a character?

      我无法理解如果要打印uint8_t变量的值将被打印为字符的逻辑.

      I can not understand the logic that if I want to print the value of a uint8_t variable, it will be printed as a character.

      P.S.我正在使用MSVS 2013.

      P.S. I am using MSVS 2013.

      推荐答案

      此行为是否是标准行为

      Is this behavior a standard

      此行为是标准行为,因为如果uint8_tunsigned char的typedef,则它将始终打印字符,因为std::ostream具有unsigned char的重载,并将变量的内容作为字符打印出来.

      The behavior is standard in that if uint8_t is a typedef of unsigned char then it will always print a character as std::ostream has an overload for unsigned char and prints out the contents of the variable as a character.

      不是最好的方法来定义uint8_t来保证将其处理为数字而不是字符吗?

      Should not be a better way to define uint8_t that guaranteed to be dealt with as a number not a character?

      为此,C ++委员会将不得不引入一个新的基本类型.当前,唯一具有sizeof()等于1的类型是charsigned charunsigned char.可能他们可以使用bool,但是bool的大小不必为1,则您仍然在同一条船上,因为

      In order to do this the C++ committee would have had to introduce a new fundamental type. Currently the only types that has a sizeof() that is equal to 1 is char, signed char, and unsigned char. It is possible they could use a bool but bool does not have to have a size of 1 and then you are still in the same boat since

      int main()
      {
          bool foo = 42;
          std::cout << foo << '\n';
      }
      

      将打印1,而不是42,因为任何非零都为true,并且true打印为1,但为默认值.

      will print 1, not 42 as any non zero is true and true is printed as 1 but default.

      我并不是说这无法完成,但是对于可以通过

      I'm not saying it can't be done but it is a lot of work for something that can be handled with a cast or a function

      C ++ 17引入了 std::byte ,其定义为enum class byte : unsigned char {};.因此它将是一个字节宽,但不是字符类型.不幸的是,由于它是enum class,所以它有其自身的局限性.已经为它定义了按位运算符,但是没有内置的流运算符,因此您需要定义自己的输入和输出.这意味着您仍在转换它,但至少您不会与unsigned char的内置运算符发生冲突.这给你类似的东西

      C++17 introduces std::byte which is defined as enum class byte : unsigned char {};. So it will be one byte wide but it is not a character type. Unfortunately, since it is an enum class it comes with it's own limitations. The bit-wise operators have been defined for it but there is no built in stream operators for it so you would need to define your own to input and output it. That means you are still converting it but at least you wont conflict with the built in operators for unsigned char. That gives you something like

      std::ostream& operator <<(std::ostream& os, std::byte b)
      {
          return os << std::to_integer<unsigned int>(b);
      }
      
      std::istream& operator <<(std::istream& is, std::byte& b)
      {
          unsigned int temp;
          is >> temp;
          b = std::byte{b};
          return is;
      }
      
      int main()
      {
          std::byte foo{10};
          std::cout << foo;
      }
      

      这篇关于std :: cout将uint8_t作为字符处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆