简体   繁体   English

如何在C ++中将UINT8转换为UINT32?

[英]How do you convert UINT8 to UINT32 in C++?

I have a value of type UINT8 and I would like to make it UINT32 . 我有一个UINT8类型的值,我想把它作为UINT32

Would my following code be considered correct, valid, efficient and safe? 我的以下代码是否会被认为是正确,有效,高效和安全的?

UINT32 convU8toU32(UINT8 *number) {
  UINT32 result = *number;
  return *result;
}

Please notice that I'm a new comer to C++ from the Java world. 请注意,我是来自Java世界的C ++的新角色。

The function is correct as is (the typo with the * in the return *result; aside), but you don't even need it. 函数是正确的( return *result;*的错误return *result;除了),但你甚至不需要它。 Integers (and other integral types) convert implicitly to one another, and as UINT32 can represent every value a UINT8 can have, you can simply write 整数(和其他整数类型)隐式转换为另一个,并且由于UINT32可以表示UINT8可以拥有的每个值,您可以简单地写

UINT32 target = source;

for some UINT8 source . 对于一些UINT8 source

Making the conversion explicit with a static_cast is optional; 使用static_cast显式转换是可选的; if the conversion was (potentially) narrowing, the cast would silence some compiler warnings. 如果转换(可能)缩小,则演员会使一些编译器警告静音。

You need 你需要

 UINT32 result = static_cast<UINT32>(*number);

to de-refernce the pointer and cast it to the right type 取消引用指针并将其转换为正确的类型

But would 但愿意

UINT32 convU8toU32(UINT8 number) {
   return static_cast<UINT32>(number);
}

be better and avoid pointers in the first place 更好,并首先避免指针

Event better avoid the function call and type in the static cast in the appropriate line of code. 事件最好避免函数调用并在相应的代码行中键入静态强制转换。

 UINT32 convU8toU32(UINT8 *number) { UINT32 result = *number; return *result; } 

I assume in my answer that UINT32 and UINT8 are fancy aliases for fundamental integer types. 我在我的回答中假设UINT32UINT8是基本整数类型的花式别名。

Would my [...] code be considered 我的代码是否会被考虑

  • correct, valid 正确,有效

No, given my assumption that UINT32 is an integer. 不,假设UINT32是一个整数。 You cannot dereference an integer. 您不能取消引用整数。 Which is what you try to do on line return *result; 这是你尝试在线return *result;

  • efficient 高效

Does not matter since it is not correct. 没关系,因为它不正确。

  • safe 安全

Well, it safely fails to compile. 好吧,它安全地无法编译。


This should be OK: 这应该没问题:

UINT32 convU8toU32(UINT8 number) {
    return number;
}

Of course, this is so simple that you may want to consider not calling the function, but assign directly in the first place: 当然,这很简单,你可能想要考虑不调用函数,但首先直接分配:

// not UINT32 foo = convU8toU32(some_uint8);
// but instead:
UINT32 foo = some_uint8;

Well, no. 好吧,不。 Assuming UINT32 is a 32-bit unsigned integral type and UINT8 is an 8-bit unsigned integral type, your code 假设UINT32是32位无符号整数类型而UINT8是8位无符号整数类型,则代码

UINT32 convU8toU32(UINT8 *number) {
   UINT32 result = *number;
   return *result;
}

would not even compile. 甚至不会编译。 The reason is that an integral type cannot be dereferenced as if it is a pointer. 原因是整数类型不能被解除引用,就像它是指针一样。 The statement return *result will therefore not compile, let alone be executed. 因此语句return *result不会编译,更不用说执行了。

In reality, converting an 8-bit unsigned integral value to a 32-bit unsigned integral type is perfectly simple. 实际上,将8位无符号整数值转换为32位无符号整数类型非常简单。

UINT32 convU8toU32(UINT8 number)
{
   UINT32 result = number;
   return result;
}

or, even more simply, 或者,更简单地说,

UINT32 convU8toU32(UINT8 number)
{
   return number;
}

These rely on implicit conversions to 32-bit unsigned integral type. 这些依赖于对32位无符号整数类型的隐式转换。 Since a 32-bit unsigned integral type can exactly represent every value that an 8-bit unsigned integral type can, the conversion preserves value. 由于32位无符号整数类型可以精确地表示8位无符号整数类型可以包含的每个值,因此转换保留了值。 Conversion the other way (from 32-bit to 8-bit) potentially loses value. 另一种方式(从32位到8位)的转换可能会失去价值。

If you want to avoid implicit conversions, simply do an explicit conversion, such as 如果您想避免隐式转换,只需进行显式转换,例如

UINT32 convU8toU32(UINT8 number)
{
    return (UINT32) number;    // C-style conversion - discouraged in C++
}

or 要么

UINT32 convU8toU32(UINT8 number)
{
    return UINT32(number);
}

or (to really make it obvious to anyone looking, and easy to find when searching a source file) 或者(对于任何看起来很明显的人来说,在搜索源文件时很容易找到)

UINT32 convU8toU32(UINT8 number)
{
    return static_cast<UINT32>(number);
}

Of course, a function isn't even needed 当然,甚至不需要功能

 UINT8 value8 = something();
 UINT32 value32 = value8;

will do instead of using this function. 我会做而不是使用这个功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM