gpt4 book ai didi

c++ - 将 `uint8_t`(从套接字读取)隐式转换为 `char`是否安全?

转载 作者:行者123 更新时间:2023-12-01 14:52:13 25 4
gpt4 key购买 nike

我对C++转换规则感到困惑,因为这些规则涉及无符号对符号,反之亦然。
我正在从套接字读取数据并将其保存在std::vector<uint8_t>中。然后,我需要阅读其中的一部分
(假设它是ASCII数据)并将其保存在std::string中。这就是我在做什么:

for (std::vector<uint8_t>::const_iterator it = payload.begin() + start; it < payload.begin() + end; ++it) {
store_name.push_back(*it);
}
如您所见, *it返回 uint8_t并将其传递到 push_backstd::string成员函数中,该函数使用 char-从而发生隐式转换。 char实际上可以是已签名或未签名的。我不确定签名是否会发生。
我无法(不打算用双关语)把头包裹在这里发生的事情以及它是否安全。 store_name.push_back(*it)在将其存储在 *it中之前是否会更改 std::string的位模式?
到底有什么规则支配?
我在网上遍历了很多地方,介绍了类型转换规则,但是它仍然没有真正适用于我。解释将不胜感激。
编辑:作为另一种表达方式- ,一般来说,当我们将unsigned转换为signed时,会发生什么?
unsigned char a = 50; // Inside the range of signed char
signed char b = (signed char) a;
b中的位模式是否需要与 a中的位模式相同?还是位模式会改变?
同样,相反的方向呢:
a = (unsigned char) b;
再次-是否会更改位模式?还是只要值在正确的范围内,无论我们执行多少有符号/无符号转换,都可以保证基础位模式保持不变?
它是使用 (cstyle cast)还是 static_cast<>的显式转换,还是通过赋值的隐式转换,这有关系吗?

最佳答案

implicit conversions - Numeric Conversion/Integral conversions:
到未签名的

If the destination type is unsigned, the resulting value is thesmallest unsigned value equal to the source value modulo 2n where nis the number of bits used to represent the destination type. That is,depending on whether the destination type is wider or narrower, signedintegers are sign-extended[footnote 1] or truncated and unsignedintegers are zero-extended or truncated respectively.


要签名

If the destination type is signed, the value does not change if thesource integer can be represented in the destination type. Otherwisethe result is implementation-defined (until C++20)the unique value ofthe destination type equal to the source value modulo 2n where n isthe number of bits used to represent the destination type. (sinceC++20). (Note that this is different from signed integer arithmeticoverflow, which is undefined).


因此,对于范围内的值,不应进行转换。否则,我将其解释为好像您的机器将值表示为2的补码一样,在转换为无符号(从C++ 20也转换为有符号)和实现直到C++ 20之前定义的位中没有任何更改。 (我不确定为什么,但是我假设大多数编译器即使允许也不会更改该值)。

关于 cstyle-caststatic-cast :cstyle-cast表演( link)

When the C-style cast expression is encountered, the compilerattempts to interpret it as the following cast expressions, in thisorder:

a) const_cast<new_type>(expression);

b) static_cast<new_type>(expression), with extensions: pointer orreference to a derived class is additionally allowed to be cast topointer or reference to unambiguous base class (and vice versa) evenif the base class is inaccessible (that is, this cast ignores theprivate inheritance specifier). Same applies to casting pointer tomember to pointer to member of unambiguous non-virtual base;

c) static_cast (with extensions) followed by const_cast;d) reinterpret_cast<new_type>(expression);

e) reinterpret_cast followed> by const_cast. The first choice that satisfies the requirements of the respective cast operator is selected, even if it cannot be compiled.


因此,对于 signed<->unsiged转换, cstyle-cast应该与 static_cast相同。

对于 隐式转换( implicit conversions - Order of the conversions)
隐式转换顺序按以下顺序组成:
  1. zero or one standard conversion sequence;
  2. zero or one user-defined conversion;
  3. zero or one standard conversion sequence.

,在哪里

A standard conversion sequence consists of the following, in thisorder:

  1. zero or one conversion from the following set: lvalue-to-rvalueconversion, array-to-pointer conversion, and function-to-pointerconversion;
  2. zero or one numeric promotion or numeric conversion;
  3. zero or one function pointer conversion; (since C++17) 4) zero or onequalification adjustment.

而数字转换又是顶部引用的转换。 static_cast本身使用隐式和用户定义的转换( link)的组合在类型之间进行转换。因此,隐式或显式之间不应有任何区别。

关于c++ - 将 `uint8_t`(从套接字读取)隐式转换为 `char`是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62502283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com