mercredi 28 octobre 2020

Why does std cin work only when used with int variable?

I'm trying to use std::cin after a while.

Using uint8_t or unsigned char:

unsigned char data;
std::cin >> std::dec >> data;

Whatever std::dec is used or not, I get the first ASCII character I type. If I type 12, data is 0x31 not 12. Why can't it parse number until 255 to be stored in a char?

int data;
std::cin >> std::dec >> data;

gives correctly data=12/0xC not 0x31

  • Why?

Using char[N] with std::hex

char data[128];
std::cin >> std::hex >> data;

Also gets the ASCII characters instead of the hexadecimal.

  • Writting 0x010203040506... data is 0xFFFFFFFFF...

  • Isn't std::cin>>std::hex able to parse the string I type into hexadecimal automatically?

Aucun commentaire:

Enregistrer un commentaire