How do you read an unsigned char?

I need to read in bytes and I can't get this to work:

unsigned char ascii;

while(cin.get(ascii))
{
...
}

I tried to static_cast<char>(ascii) but that didn't work. Anyone know the problem?

UPDATE:

I am trying to read 8-bit characters.

I tried static casting because I just wanted the get() to work even though it was counter intuitive.

while(ascii = cin.get())

Causes an infinite loop, not sure how to fix.
Last edited on
Some compilers have the option of having the char unsigned by default.
btw, you can do this:
1
2
3
4
5
6
unsigned char ascii;

while ( ascii = cin.get() )
{
   //...
}
Define "byte". Are you reading hex bytes? Characters?

Why, if your question is how to read an unsigned char, are you
attempting to static cast to (signed) char?
Er, that loop may not terminate...

The STL prototypes on char (unfortunately), so you need to cast the appropriate way:
1
2
3
4
while (cin.get( reinterpret_cast <char&> (ascii) ))
  {
  //...
  }

Hope this helps.
I believe that some architectures (like M68000) don't allow you to mix pointers to signed/unsigned.
Topic archived. No new replies allowed.