Jump to content
  • 0

Need an explanation


Kazegaya
 Share

Question

Hello :)

 

I've recently started a small project in order to improve my programming skills : an OoT save editor.

 

First, I must say you that my question may be very stupid but since I'm a newbie in programming, it's not clear for me :)

 

Anyway, maybe I'm just stupid but why are the datas stored backward ? I mean, for example let's take these numbers >

1234 5678. According to what I have noticed, If they were in this file, they would be in this order, > 4321 8765. It's very troublesome for me since I've just started programming so maybe it's normal for a good programmer but I just can't "think backward" without getting a headache ^^ I then tried to write a small program that puts them forward but, of course, the save file is no longer recognised.

 

So, could you explain me why the datas are stored that way ? There must be a reason, mustn't it ?

 

Thank you !

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

It's due to endianess. Here's a function that I have written in C++ (it isn't entirely mine, I've just modified it by adding templates so you can use arguments of 16-bit, 32-bit, and 64-bit) that converts from little-endian (example: "4321 8765") to big-endian (normal order) if needed:

 

#include <typeinfo>
#include <stdint>

// Endian Checking Macros
const int i = 1;
#define IS_BIGENDIAN() ( (*(char*)&i) == 0 )

template <class T>
T ReverseOffset(T Offset)
{
        unsigned char c1, c2, c3, c4, c5, c6, c7, c8;

        if (IS_BIGENDIAN())
            return Offset;
        else
        {
            if (typeid(T).name() == typeid(uint64_t).name())
            {
                c1 = Offset & 0xFF;
                c2 = (Offset >> 8) & 0xFF;
                c3 = (Offset >> 16) & 0xFF;
                c4 = (Offset >> 24) & 0xFF;
                c5 = (Offset >> 32) & 0xFF;
                c6 = (Offset >> 40) & 0xFF;
                c7 = (Offset >> 48) & 0xFF;
                c8 = (Offset >> 56) & 0xFF;

                return ((T)c1 << 56) + ((T)c2 << 48) + ((T)c3 << 40) + ((T)c4 << 32) + ((T)c5 << 24) + ((T)c6 << 16) + ((T)c7 << 8) + c8;
            }
            else if (typeid(T).name() == typeid(uint32_t).name())
            {
                c1 = Offset & 0xFF;
                c2 = (Offset >> 8) & 0xFF;
                c3 = (Offset >> 16) & 0xFF;
                c4 = (Offset >> 24) & 0xFF;

                return ((T)c1 << 24) + ((T)c2 << 16) + ((T)c3 << 8) + c4;
            }
            else if (typeid(T).name() == typeid(uint16_t).name())
            {
                c1 = Offset & 0xFF;
                c2 = (Offset >> 8) & 0xFF;

                return (c1 << 8) + c2;
            }
        }

        return Offset;
}

By the way, spinout has already written a save file editor: http://64.vg/src/c9b6ce6071c84b8ed03b5844ea80632d

Either way, good luck to any future programs you are writing :)

Link to comment
Share on other sites

 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.