Archived

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

gimp

Endian Flip

Recommended Posts

gimp    142
I''m attempting to grab a number from a big endian data stream like this:
  
inline const unsigned int CLightwaveModel::GetU4(void)
{
	//Advance the index

	m_DataIndex += sizeof(unsigned int);

	//Return the value

	return 	(((char)m_Data[m_DataIndex-4]<<24)
 		    +((char)m_Data[m_DataIndex-3]<<16)
		    +((char)m_Data[m_DataIndex-2]<<8)
			+(char) m_Data[m_DataIndex-1]);	
}
  
However I can''t seem to work out the exact syntax. I see the data like this: 00 00 01 90 A quick check in calc tells me this is 400 in decimal, which is the number I expected to get. When I run my code the above function returns 144. Can anyone see anything silly in my code? Many thanks Chris Brodie http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
MagicScript    290
When you shift, you run right out of the bounds of the char...
90 in hex is your 144 in decimal, all the other hex values are shifted out of existance.

Try this (I think):
( (unsigned int)((char)m_Data[m_DataIndex-4])<<24 )

Share this post


Link to post
Share on other sites