Cleanly analyzing and handling integers

1

I am reverse engineering an old game format. For their textures, they store display type information in a single integer. I initially though the different bits were boolean flags. For example, if bit 1 was set, the texture is transparent. The more I have worked through the different display types, the less I think that is the case. So now I am handling them as just whole integers.

Here is some sample data:

1. When the texture is invisible, the flag integer is 0.
2. Diffuse          - flag integer: -2147483647. Hex: 0x80000001
3. Semi Transparent - flag integer: -2147483643. Hex: 0x80000005
4. Masked           - flag integer: -2147483629, Hex: 0x80000013 

There are quite a few more types and they have their own unique integer value as well.

What is the easiest way to handle this data? An if/else statement with the huge negative integers is super ugly and hard to read. It looks like it would be easier to analyze them as hex values but each if/else statement requires an int.Parse statement like so:

if (parameters == int.Parse("0x80000001", NumberStyles.HexNumber))
{
    // Handle this case            
}

Is there any other suggested clean way of doing this and analyzing the data? Is it possible to just analyze the end of the hex value somehow?

c#
type-conversion
hex
reverse-engineering
asked on Stack Overflow Feb 5, 2019 by Satchmo Brown • edited Feb 5, 2019 by Satchmo Brown

1 Answer

1

There are quite a few more types and they have their own unique integer value as well...What is the easiest way to handle this data?

What you are describing is known as magic numbers. i.e. whenever your code needs to deal with arbitrary almost random numbers and it is not clear as to their meaning.

The best way to deal with them is to define them as constants once with comments and then to use the constants in code rather than repeating the “ugly” numbers throughout the code base.

Perhaps something like:

public static class TextureConstants
{
    // TODO comments
    public static int Diffuse = -2147483647;
    public static int SemiTransparent = -2147483643;
    public static int Masked= -2147483629;
}

Also their is no reason why the above could not have been done with an enum but if you are going to do lots of int comparison, comparing int with an enum requires pesky casting.

An if/else statement with the huge negative integers is super ugly and hard to read

Later you can use it in if or switch statements as so:

switch (someNumber)
{
    case TextureConstants.Diffuse:
        // do something
        break;

    case TextureConstants.SemiTransparent:
        // do something
        break;

    case TextureConstants.Masked:
        // do something
        break;
}

Dictionary

If you don't like switch statements you can use a Dictionary<>:

public enum Textures
{
    Diffuse,
    SemiTransparent,
    Masked
}

// initialise the dictionary
Dictionary<int, Textures> dict;
dict[TextureConstants.Diffuse] = Textures.Diffuse;
dict[TextureConstants.SemiTransparent] = Textures.SemiTransparent;
dict[TextureConstants.Masked] = Textures.Masked;

int someNumber = // ...
if (dict.TryGetValue (someNumber, out var texture))
{
    // got one
    if (texture == Textures.Diffuse) // ...
}
answered on Stack Overflow Feb 5, 2019 by MickyD • edited Feb 5, 2019 by MickyD

User contributions licensed under CC BY-SA 3.0