Type casting/conversion from unsigned long to int32

Hey,
At this point in the course I dont believe we’ve learned how to convert or cast one type to another, but I’m having an issue when trying to implement GetHiddenWordLength.

It seems that MyHiddenWord.length() returns a value of type unsigned long, but we’re wanting to use it as an int32. I did some reading up on converting between the two and it seems that this works: return (int32)MyHiddenWord.length(); but I wasn’t sure if that was the expected implementation

2 Likes

Yep, this worked. I had the same issue.
Still unsure if that’s what it is supposed to be.
Thanks!

1 Like

Hello @rossvz,

interesting observation. .length() is definitely returning an integer value.
Maybe @ben or @sampattuzzi would have an answer :wink: :blush:

Kind regards
OtenMoten

What did we define int32 as in this project?

C++ int sizes can be very confusing. Basically there are 4 distinct types:

  • short (at least 16-bit)
  • int (at least 16-bit)
  • long (at least 32-bit)
  • long long (at least 64-bit)

Each can either be signed or unsigned (meaning that higher numbers can be represented at the expense of not representing negative ones).

However, hardware can only represent 16, 32 and 64-bit integers. So the choice of the actual representation is platform specific.

This is why we can’t automatically convert an unsigned long to int32. We might be losing information.

More on integers in C++ here.

2 Likes

Hey @sampattuzzi,

thanks for the fast response. I was re-reading my sentence and maybe the following explanation with my own words is more clear:

When talking about int32 we are talking about using int32 = int;. So basically, this means that unsigned int = int, and this is NOT possible. Therefore, you need to convert.

Cheers
Kevin

Yup. Definitely need to convert an unsigned to signed and vice versa.

1 Like

Privacy & Terms