Ridiculous problem reading polyglot book entries.

Discussion of chess software programming and technical issues.

Moderator: Ras

P. Villanueva
Posts: 85
Joined: Sat May 17, 2008 10:57 pm
Location: Bilbao, Spain

Ridiculous problem reading polyglot book entries.

Post by P. Villanueva »

I'm writing code in order to make my engine compatible with polyglot books.
Mi first step is writing a function that read entries from a book. I wrote this:

Code: Select all

while (!f.eof()) {
           f.read((char *)&key, 8); 
           f.read((char *)&move, 2);
           f.read((char *)&weight, 2);
           f.read((char *)&learn, 4);
           cout << key << " " << move << " " << weight << " " << learn << "\n";
           cin.get();
     }
This generates an output like this:

Code: Select all

51ea9a62840e0e00 b70b 600 0
95e456746f1c1200 610c 800 0
4ee9d4785c581400 b30e c00 0
4ee9d4785c581400 6d0d 900 0
e4b1441b92851400 2407 400 0
First entry should be:
000e0e84629aea51 0bb7 0006 0

It's reading the data backwards! :oops:

What am I doing wrong? Have I to read data char by char?
Thanks a lot
Gian-Carlo Pascutto
Posts: 1260
Joined: Sat Dec 13, 2008 7:00 pm

Re: Ridiculous problem reading polyglot book entries.

Post by Gian-Carlo Pascutto »

Read up on little endian vs big endian byte orders.
P. Villanueva
Posts: 85
Joined: Sat May 17, 2008 10:57 pm
Location: Bilbao, Spain

Re: Ridiculous problem reading polyglot book entries.

Post by P. Villanueva »

Thanks, looking little endian big endian in google i've foung this

http://www.codeguru.com/forum/showthread.php?t=292902

Code: Select all

inline void endian_swap(unsigned short& x)
{
    x = (x>>8) | 
        (x<<8);
}

inline void endian_swap(unsigned int& x)
{
    x = (x>>24) | 
        ((x<<8) & 0x00FF0000) |
        ((x>>8) & 0x0000FF00) |
        (x<<24);
}

// __int64 for MSVC, "long long" for gcc
inline void endian_swap(unsigned __int64& x)
{
    x = (x>>56) | 
        ((x<<40) & 0x00FF000000000000) |
        ((x<<24) & 0x0000FF0000000000) |
        ((x<<8)  & 0x000000FF00000000) |
        ((x>>8)  & 0x00000000FF000000) |
        ((x>>24) & 0x0000000000FF0000) |
        ((x>>40) & 0x000000000000FF00) |
        (x<<56);
}
Problem solved.