Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Not sure how to read write correctly with sf::Uints?  (Read 1533 times)

0 Members and 1 Guest are viewing this topic.

Austin J

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Not sure how to read write correctly with sf::Uints?
« 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.
« Last Edit: June 01, 2014, 05:29:45 am by Austin J »

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Not sure how to read write correctly with sf::Uints?
« Reply #1 on: June 01, 2014, 06:00:00 am »
It's shift by 8, 16 and 24, you're doing 8, 16 and 32.
Back to C++ gamedev with SFML in May 2023

 

anything