Sign in to follow this  

Binary to Integer (Read hex binary file)

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

I'm trying to read an unknown type file (a save game file, I change the value and then see the file in Hex editor) !

And i want to read the integer values. I know where it is ! but the hex value is quite strange! I use Decimal here for easy understanding!

 

(byte-byte -> integer )
           //128,63 -> 1           128 =  10000000   63=00111111
            //0, 64 -> 2(10)            00000000-0100000064=    01000000
            //64-64 -> 3(11)            01000000-01000000
            //128-64 -> 4(100)          10000000-01000000
            //160-64 -> 5(101)      160=10100000-01000000
            //192-64 -> 6(110)      192=11000000-01000000
            //224-64 -> 7(111)          11100000-01000000
            //0-65 -> 8 (1000)          00000000-01000001
            //16-65 -> 9(1001)          00010000-01000001   
            //32-65 ->10(1010)          00100000-01000001
            //48-65 ->11(1011)          00110000-01000001   
            //64-65 ->12(1100)          01000000-01000001
            //80-65 ->13(1101)          01010000-01000001
            //96-65 ->14(1110)          01100000-01000001   
            //112-65 ->15(1111)         01110000-01000001
            //128-65 ->16(10000)        10000000-01000001
 
            //136-65 ->17(10001)        10001000-01000001   
            //144-65 ->18(10010)        10010000-01000001
            //152-65 ->19(10011)        10011000-01000001
 
            //160-65 ->20(10100)        10100000-01000001   
            //168-65 ->21(10101)        10101000-01000001
            //176-65 ->22(10110)        10110000-01000001
 
            //32-66 ->40(101000)        00100000-01000010
 
            //48,64,80
 
            //160-66 ->80(1010000)      10100000-01000010   
            //162-66 ->81(1010001)      10100010-01000010
            //164-66 ->82(1010010)      10100100-01000010
 
 
(bigger values, int,int,int -> byte-byte )
//80,81,82  -> 160-66,162-66,164-66
            //180,181,182 -> : 52-67,53-67,54-67
            //20,21,22 -> 160 ,168 , 176 -65
            //30,40,50 ->  240-65,248-65, 0-66  (3 integer -> 3 (2bytes))
 
//128-191 -> -1             10000000-10111111
            //0-192 -> -2               00000000-11000000
            //64-192 -> -3              01000000-11000000
 
The values is like above. It use 4 byte (but only 2 byte have values) . It seems that the left-most bit of 2nd byte are for Signed!
There is some rule , but i find it hard to figure out !
Thanks !
 
Edited by greenpig83

Share this post


Link to post
Share on other sites

Your notation is extremely cryptic and you do not explain anything - i would be amazed if anyone would be able to guess what you are talking about.

 

Anyway, just in case, this is how values are stored on your typical little-endian format:

 

(2 byte integer)

original value: 4660 -(hex)-> 0x1234 -(as bytes)-> 0x34, 0x12

original value: -4660 -(hex)-> 0xEDCC -(as bytes)-> 0xCC, 0xED

Note: 0xEDCC = 0x10000 - 0x1234

(4 byte integer)

original value: -123456789 -(hex)-> 0xF8A432EB -(as bytes)-> 0xEB, 0x32, 0xA4, 0xF8

 

edit:

If i understood your notation - looks like floating point to me. Treat it as a standard 4 byte float - see if that does the trick.

Edited by tanzanite7

Share this post


Link to post
Share on other sites
It's a 16-bit floating point type.

#include <cstdio>
#include <cmath>

void print_as_tinyfloat(int i) {
  int sign = (i < 0);
  int exponent = static_cast<int>(std::floor(std::log2(std::abs(i))));
  int mantissa = (i << (7 - exponent)) ^ (1 << 7);
  unsigned tinyfloat = (sign << 15) + ((exponent + 127) << 7 ) + mantissa;

  std::printf("%d -> %d %d\n", i, tinyfloat % 256, tinyfloat / 256);
}

int main() {
  for (int i=1; i<20; ++i)
    print_as_tinyfloat(i);
}

Share this post


Link to post
Share on other sites

Wow, never think of that. I just think inside the box that it must be integer!

I'll try and see if it's right ! Spend hours to figure a very simple thing sad.png

 

Yeah. that it ! Using C# it's more simple !

 

byte []t={0,0,32,65};
float myFloat = System.BitConverter.ToSingle(t, 0); //-> return 10
 
Because the game only let me input integer value, so I think that it's integer !
Edited by greenpig83

Share this post


Link to post
Share on other sites

It's a 16-bit floating point type.

Half-float does not have an 8-bit exponent tongue.png. The realization that an exponent could explain the related change in the byte pairs and the one bit crossing the byte border (due to the sign bit) was the smoking gun for me. The exponent encoding works as a fair giveaway too, but missed it at first (64 = 128 >> 1).
 

Because the game only let me input integer value, so I think that it's integer !

This has been actually very common occurrence i my savegamemucking endeavors. Even with values where having a float makes no sense - ie. the value can logically only be whole numbers (a'la unit count etc).

Edited by tanzanite7

Share this post


Link to post
Share on other sites
I thought for a minute that it was a strange custom 16-bit float with 8-bit exponent and 7-bit mantissa, but what's really going on is that it's a traditional 32-bit float, and the OP only gave us the 2 non-zero bytes (and I missed the comment about the two zero bytes). The other two bytes are always zero because for small integers they represent things after the point.

So just forget the code I posted. The answer is that it's a float, as tanzanite7 pointed out. Edited by Álvaro

Share this post


Link to post
Share on other sites

This topic is 1427 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this