C/C++ tips Home

C/C++ tips

Remove suffix from std::string

            std::string str = "foo.pdf";
            std::cout << str.substr(0, str.find_last_of('.')) << std::endl;
            // foo
            

Bit shift from byte

            char buf[2] = { 0xfe, 0x07 };
            unsigned int x = (buf[0] << 24); x = x >> 24;
            unsigned int y = (buf[1] << 24); y = y >> 16;
            unsigned int val = x + y;
            unsigned int val_2 = buf[0] + (buf[1] << 8);
            std:cout << val << std::endl; // 2046
            std:cout << val_2 << std::endl; // 1790