Advertisement Jump to content


This topic is now archived and is closed to further replies.


Blind casing in an array

This topic is 6579 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

I have an array of unsigned chars, read in from a file. I want to extract various data from the file. Is this ok? mylong = (unsigned long)&a_Array[a_Index]; Thanks gimp

Share this post

Link to post
Share on other sites
maybe yes, maybe no

remember that C++ automatically performs base numeric type conversions. If you try to suck a raw binary float, currently held as BYTEs, it will convert the spurious DWORD value into its corresponsing floating point value - which will assuredly not be the same as a raw C type cast (which may be desired in this case).

if you cast the float to an unsigned long before you spit it out, then it will kinda sort work, but you''ll lose the floating part. If you had an record or array of stuff and cast a DWORD (or BYTE) pointer to it, then wrote that it, you have to suck it back in that way too.

It also will not work on integral types, because of the way x86s store stuff in ram. The upper & lower words will get swapped around (this makes entering text into ram with debug woefully difficult ''EHLL OOWLR D'').

Magmai Kai Holmlor
- The disgruntled & disillusioned

Share this post

Link to post
Share on other sites
Thanks for the info. I ended up solving the problem by reading in the bytes individually then gluing them together. That seems to have done the trick...

inline const unsigned long GetU4(CArray &a_Array, unsigned long &a_Index)
//Advance the index

a_Index += 4;

//Return the value

return (((unsigned char) a_Array[a_Index-4]<<24)
+((unsigned char) a_Array[a_Index-3]<<16)
+((unsigned char) a_Array[a_Index-2]<<8)
+(unsigned char) a_Array[a_Index-1]);

PS : The data I''m reading in is theoretically big endian as well...



Share this post

Link to post
Share on other sites
mylong = (unsigned long)&a_Array[a_Index];

is wrong, but you''re nearly there ;-)

mylong = *((unsigned long*)&a_Array[a_Index]);

You have to turn the point you need to access in the byte array to an unsigned long pointer, then get the value at that address.

Don''t forget:

a_Index += sizeof(unsigned long);


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!