Code:
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

main () {
    cout << hex << setw(2) << setfill('0') << 0x00 << endl;
    cout << hex << setw(2) << setfill('0') << 0x07 << endl;
    cout << hex << setw(2) << setfill('0') << 0xFF << endl;
}
This code prints:
00
07
ff

Try using setw and setfill instead of setprecision.