0

I am in the midst of debugging someone's code, and I have code like

int i = their_val;
std::cout << "output: " << i << std::endl;

When I look at the log output I see lines like

output: a

Should this happen? Is something changing the cout formatting or could it be something odder?

2
  • 1
    "causing integers to become hex numbers" is a phrase to think about... those poor integers.
    – Kerrek SB
    Oct 31, 2011 at 17:25
  • As the answers below have noted, a preceding std::hex is causing the issue. See this previous discussion: stackoverflow.com/questions/1532640/…
    – Gnawme
    Oct 31, 2011 at 18:20

3 Answers 3

3

Did someone cout << std::hex prior to that output? It would cause it to print in hexadecimal.

1
  • I think it's probably that (upvoted) but it's a huge enough code base that I can't immediately find it. I realised this was probably the case just after submitting... Oct 31, 2011 at 17:27
3

Check and see if std::hex gets passed into std::cout anywhere. That would result in the behavior you're seeing.

You can force things to be in decimal using:

std::cout << "output: " << std::dec << i << std::endl;
0

You probably did std::cout << std::hex somewhere earlier. You can undo this with std::cout << std::dec.

std::cout << "output: " << std::dec << i << std::endl;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.