Well, no. Assuming UINT32
is a 32-bit unsigned integral type and UINT8
is an 8-bit unsigned integral type, your code
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.
In reality, converting an 8-bit unsigned integral value to a 32-bit unsigned integral type is perfectly simple.
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. Since a 32-bit unsigned integral type can exactly represent every value that an 8-bit unsigned integral type can, the conversion preserves value. Conversion the other way (from 32-bit to 8-bit) potentially loses value.
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.