Jump to content
  • Advertisement
Sign in to follow this  

[.net] Odd problem with a variable not having the same value?

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

Hm. I must admit I haven't programmed in a while so this might be a really silly problem, but I can't seem to figure it out!
        private static void GetIffTypes(BinaryReader Reader)
            List<IffType> Types = new List<IffType>();

            Reader.BaseStream.Seek(64, SeekOrigin.Begin);

            while (Reader.BaseStream.Position < Reader.BaseStream.Length)
                IffType T = new IffType();

                T.Offset = (int)Reader.BaseStream.Position;
                T.Type = new string(Reader.ReadChars(4));
                T.Size = ConvertFromCharArray(Reader.ReadChars(4));
                T.ID = ConvertFromCharArray(Reader.ReadChars(2));
                T.TypeNum = ConvertFromCharArray(Reader.ReadChars(2));
                T.Label = new string(Reader.ReadChars(64));
                T.DataOffset = T.Offset + 76;
                T.DataSize = (T.Size - 76);
                T.Data = Reader.ReadBytes(T.DataSize);

                Console.WriteLine("Type: " + T.Type.ToString());
                Console.WriteLine("Size: " + T.Size.ToString());
                Console.WriteLine("ID: " + T.ID.ToString());
                Console.WriteLine("TypeNum: " + T.TypeNum.ToString());
                Console.WriteLine("Label: " + T.Label + "\n");
In the above code, program execution crashes on the line: T.Data = Reader.ReadBytes(T.DataSize); Because T.DataSize is (apparently) -76! The value of T.Size as printed to the console comes out as 70397, but whenever I check the value in the Debugger, it is 0. Which means that T.DataSize ends up at -76. How is it possible that the value of T.Size differs internally from what it's value is said to be in the console?! Here's the rest of the relevant code:
    public class IffType
        public string Type = "";
        public int Offset = 0;
        public int Size = 0;
        public string Label = "";
        public int ID = 0;
        public int TypeNum = 0;
        public int DataOffset = 0;
        public int DataSize = 0;
        public byte[] Data;

        private static int ConvertFromCharArray(char[] CharArray)
            int result = 0;

            for (int i = 0; i < CharArray.Length; ++i)
                result += (CharArray[(CharArray.Length - 1) - i] << (i * 8));

            return result;

Share this post

Link to post
Share on other sites
My first guess is that as your write statements happen after the line that throws you are simply seeing the number printed by the previous iteration and not the value that causes the exception. Check for bad values of your DataSize variable before making your read call. As you have it now, if the read call throws the following lines will not be executed.

hope this helps.

Share this post

Link to post
Share on other sites
In addition you might try to flush the output buffer, to force it to write the whole content. I don't remember right now if thie is automatically done...

Share this post

Link to post
Share on other sites
Thanks guys, I figured it out! :)
Turns out I had to use this function to read T.DataSize;

long HexToInt(byte[] Buffer, int Bytes)
//Big Endian (Left = Biggest)
//Reads from right to left (most RIGHT # is x*256^0)
long Result = 0;
byte Character;
if (Bytes &gt; 8)
{return -1;}
for (int i = 0; i &lt; Bytes; i++)
Character = Buffer[Bytes - 1 - i];
Result = Result + (Character*Math.Pow(256,i));
return Result;

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!