Ridiculous problem reading polyglot book entries.

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

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&#40;);
     &#125;
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: 1243
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&#40;unsigned short& x&#41;
&#123;
    x = &#40;x>>8&#41; | 
        &#40;x<<8&#41;;
&#125;

inline void endian_swap&#40;unsigned int& x&#41;
&#123;
    x = &#40;x>>24&#41; | 
        (&#40;x<<8&#41; & 0x00FF0000&#41; |
        (&#40;x>>8&#41; & 0x0000FF00&#41; |
        &#40;x<<24&#41;;
&#125;

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