Trouble in serializing/deserializing int.
Hello Gurus,
I am facing trouble in serializing and deserializing int.
Code:
typedef char int_8;
int_8* SerializeInt(int value)
{
int_8* buff = new int_8[4];
*( buff++ ) = ( int_8 )( value >> 24 );
*( buff++ ) = ( int_8 )( value >> 16 );
*( buff++ ) = ( int_8 )( value >> 8 );
*( buff++ ) = ( int_8 )( value );
return buff;
}
int DeserializeInt(int_8*& m_buff)
{
int temp = *( m_buff++ );
temp = ( temp << 8 | *( m_buff++ ) );
temp = ( temp << 8 | *( m_buff++ ) );
temp = ( temp << 8 | *( m_buff++ ) );
return temp;
}
Problem is when I use SerializeInt( value) function and pass any value to be serialized and when I deserialized the returned value I get -3 always.
I am not able to locate the problem and debugger also is not of great help here..
Thanks in advance
Regards
Prashant.
Re: Trouble in serializing/deserializing int.
I think you just put an extra "&" in the prototype for DeserializeInt:
Code:
int DeserializeInt(int_8*& m_buff)
That's why, you're using the pointer to the integer instead of the value of the integer!
Re: Trouble in serializing/deserializing int.
Hello Bornish,
"&" was put so that the pointer itself is updated and point to 4 places up after the function DeserializeInt returns.
And even if I remove it seems there is no improvement..
Re: Trouble in serializing/deserializing int.
There are several problems with this code but the most serious is that you are returning the wrong pointer from SerializeInt(). You allocate an array, increment the pointer four times, and return an invalid pointer.
Re: Trouble in serializing/deserializing int.
yup you are right this is hieght of stupidity on my part..:D