Jump to content
  • Advertisement
Sign in to follow this  
PaulHolverda

casting a char buf[4] into an int

This topic is 3790 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi i need to cast a buffer that i read out at a specific location in a file, i know at that place i stored there an integer so i did this: char buf[4]; if(stream->alTestReadFile(&buf, 4)){ } where the first argument is the adress of the buffer and the second the number of bytes to read, but i need to cast it into a integer(Little endian for now) how to do this cheers, Paul

Share this post


Link to post
Share on other sites
Advertisement
	//send two unsigned ints across a socket.
char recvBuffer[8];
int numBytes=0;
while (numBytes < 8)
{
int n = recv(sd, &recvBuffer[0]+numBytes, 8-numBytes, 0);
if (n == SOCKET_ERROR)
{
//remote host unexpectedly dropped connection
return;
}
numBytes += n;
}

assert(sizeof(unsigned int)==4);
unsigned int a = *reinterpret_cast<unsigned int*>(&recvBuffer[0]);
imageWidth = ntohl(a);
unsigned int b = *reinterpret_cast<unsigned int*>(&recvBuffer[4]);
imageHeight = ntohl(b);

Share this post


Link to post
Share on other sites
I guess that's what you need:

char c[4];
int i = *reinterpret_cast<int*>(&c[0]);

Oh, and make sure, your ints really are 4 bytes long.

Share this post


Link to post
Share on other sites
yes it works, but i didn't get the correct results, but that is a problem in the octree creator. But another question arise what is exactly the difference between a cast like this (int*) and reinterpret_cast<int*>, i got exactly the same results

cheers,

Paul

Share this post


Link to post
Share on other sites
Quote:
Original post by PaulHolverda
yes it works, but i didn't get the correct results, but that is a problem in the octree creator. But another question arise what is exactly the difference between a cast like this (int*) and reinterpret_cast<int*>, i got exactly the same results

cheers,

Paul


The difference is that the (int*) is a C-style cast, and it can mean a bit of everything.
reinterpret_cast<int*> is one of the C++-style casts, and is to be preferred if you write C++ code.

reinterpret_cast has a much more precise meaning. (it re-interprets a pointer, essentially. If given a char pointer, in your case, it pretends that the address instead points to an int, so it just reinterprets the data without performing a conversion)
A C-style cast *sometimes* does this as well.

Another C++ cast is static_cast, which performs a (static) type conversion. An example is this:
static_cast<int>(4.0f);
this takes the float value 4.0f and converts it to an integer. So you get the value 4 out.
A C-style cast can do that as well ((int)4.0f)

There's also dynamic_cast, which handles inheritance and polymorphism (allows you to safely cast from a base class to a derived class, and get an exception, rather than undefined behavior, if the variable isn't an instance of the derived class)

const_cast is another, which allows you to remove the const-ness from a variable (convert a 'const int' to 'int', for example).
And again, a C-style cast can do this.

There are several nice things about the C++ casts.
- They're safer. (If you try to use static_cast on something where a static conversion is not meaningful, you get a compiler error. With a C-style cast, it'd have just performed one of the other types of casts. Which wasn't what you intended. Nasty bug.
- They better convey what it is you're trying to do. Their meaning is much more specific, so if you encounter a static_cast, you know pretty much what it's doing. If you encounter a C-style class, you have to read a lot of the surrounding code to be sure of what exactly it does, before you can even start wondering why, and if that was really the intended effect.
- They're easier to locate (you can search for reinterpret_cast, but it's really hard to search for the C-style cast.
- They're uglier [lol]
I suspect that this is a hint to remind you that "casts are kinda nasty. Don't use them if you can avoid it"

Edit:
I've left out a lot of details about the C++ casts (some of the details are pointed out below), so you might want to read up on them. I just wanted to explain roughly how they differ from plain old C casts.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!