0

Ok so logically speaking when i use my calculator on this variable padding1 i get 6B2 but in the program i get FFFFFFFFFEADF6B2 how come it adds those extra numbers on there and also newoffsets[i] * 0x800 is 0x1561000 and WAL.Position is 156094E when you get the sum of those answers you should get 6B2 why do i get this super large FFFFFFFFFEADF6B2 number WAL.Position is a long and newoffsets[i] is a int

        using (FileStream WAL = new FileStream(@"C:\Users\denar\source\repos\Sly_WAL_Editor\TEST.WAL", FileMode.Create, FileAccess.Write))
        {
            List<long> newOffsets = new List<long>();
            WAL.Write(BitConverter.GetBytes(wal.unk), 0, sizeof(uint));
            WAL.Write(BitConverter.GetBytes(wal.entries.Count), 0, sizeof(uint));
            
            uint walNextSector = maxTOCSize / 2048;

            for (int i = 0; i < 1; i++)
            {
                tocEntry = wal.entries[i];

                WAL.Write(Encoding.ASCII.GetBytes(tocEntry.folderName), 0, tocEntry.folderName.Length);
                WAL.WriteByte(0);
                WAL.Write(Encoding.ASCII.GetBytes(tocEntry.fileNames), 0, tocEntry.fileNames.Length);
                WAL.WriteByte(0);

                Alignrebuild(WAL);

                for (int b = 0; b < tocEntry.fileNames.Length; b++)
                {
                    tocFileEntry = tocEntry.files[b];
                    tocFileEntry.offset = walNextSector;
                    
                    walNextSector += (tocFileEntry.size + 0x800 - 0x1) / 0x800;

                    newOffsets.Add(tocFileEntry.offset);

                    WAL.Write(BitConverter.GetBytes(tocFileEntry.offset), 0, sizeof(int));
                    WAL.Write(BitConverter.GetBytes(tocFileEntry.size), 0, sizeof(int));
                }
            }

            long padding = maxTOCSize - WAL.Position;
            byte[] buffer = new byte[padding];

            WAL.Write(buffer, 0, buffer.Length);
            

            for (int i = 0; i < 1; i++)
            {
                tocEntry = wal.entries[i];

                for(int j = 0; j < tocEntry.fileNames.Length; j++)
                {
                    tocFileEntry = tocEntry.files[j];

                    var padding1 = (newOffsets[i] * 0x800) - WAL.Position;
                    
                    MessageBox.Show(padding1.ToString("X"));

                    byte[] buffer1 = new byte[padding1];

                    WAL.Write(buffer1, 0, buffer1.Length);
                    WAL.Write(tocFileEntry.fileData, 0, tocFileEntry.fileData.Length);
                }
            }

This is showing the super large number i get in a variable padding1

Riku786
  • 1
  • 1
  • You probably want `0x800L` to make it use the long type for the calculation. If that works, it's the same problem as in [Multiplication of two ints overflowing to result in a negative number](https://stackoverflow.com/questions/7215411/multiplication-of-two-ints-overflowing-to-result-in-a-negative-number). It would have been better to include the actual types of all the variables involved. – Andrew Morton Apr 04 '21 at 16:18
  • so i make newoffset[i] a long? – Riku786 Apr 04 '21 at 16:21
  • WAL.Position is a long and newoffsets[i] is a int – Riku786 Apr 04 '21 at 16:23
  • I cannot reproduce the problem with the values in the question and the ones we need to see aren't visible in the screenshot: are you sure they are what they are what you expect? – Andrew Morton Apr 04 '21 at 16:30
  • yes would me showing more code help – Riku786 Apr 04 '21 at 16:32
  • 1
    If you place the problematic code in a [`checked`](https://stackoverflow.com/a/20709496/1115360) block, does it throw an exception? That would be a good opportunity to have another look at the variable values in case an unexpected one has sneaked in. – Andrew Morton Apr 04 '21 at 16:47

0 Answers0