I have a value of type UINT8
and I would like to make it 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.
The function is correct as is (the typo with the *
in the return *result;
aside), but you don't even need it. 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 target = source;
for some UINT8 source
.
Making the conversion explicit with a static_cast
is optional; 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.
Would my [...] code be considered
- correct, valid
No, given my assumption that UINT32
is an integer. You cannot dereference an integer. Which is what you try to do on line 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 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.