SFML community forums

Help => General => Topic started by: Austin J on June 01, 2014, 05:25:46 am

Title: Not sure how to read write correctly with sf::Uints?
Post by: Austin J on June 01, 2014, 05:25:46 am
I went ahead and used SFML's sf::Uints for file I/O. From what I can see it's just the common typedefing such as

typedef unsigned char uint8

When trying to read and write however, I'm not getting

sf::Uint8[4]

to properly get stored within

sf::Uint32

Files are reading completely wrong, and my ide is giving warnings about me shifting wrong.

sf::Uint32 MapFileHandler::Read32()
{
        if(inFile.is_open())
        {
                sf::Uint8 bytes[4];
                sf::Uint32 value;

                inFile.read(reinterpret_cast<char *>(bytes),4);

                value = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 32);

                return value;
        }
}
 

void MapFileHandler::Write32(sf::Uint32 & u32)
{
        if(outFile.is_open())
        {
                sf::Uint8 bytes[4];

                bytes[0] = u32 & 0xFF;
                bytes[1] = (u32 >> 8) & 0xFF;
                bytes[2] = (u32 >> 16) & 0xFF;
                bytes[3] = (u32 >> 32) & 0xFF;

                outFile.write(reinterpret_cast<char *>(bytes),4);
        }
}
 

I'm not very good with handling bits themselves and haven't used these types a whole lot in the past.
Title: Re: Not sure how to read write correctly with sf::Uints?
Post by: FRex on June 01, 2014, 06:00:00 am
It's shift by 8, 16 and 24, you're doing 8, 16 and 32.