home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: unsigned char?
- Message-ID: <1992Nov14.221510.13655@taumet.com>
- Keywords: unsigned char?
- Organization: TauMetric Corporation
- References: <1992Nov13.195635.19707@imada.ou.dk>
- Date: Sat, 14 Nov 1992 22:15:10 GMT
- Lines: 37
-
- adelt@imada.ou.dk (Adel Shavandi) writes:
-
-
- >I have the following code segment:
-
- > unsigned char _number;
- > ifstream istrm;
-
- > istrm>>_number;
-
- >in the input file to be read, i am supposed to read 15, but by
- >inspection i find out that _number is 49!
-
- The conversion for char or unsigned char reads one character from
- the input, possibly first skipping leading whitespace. If the next
- input character was '1', then _number would would contain '1'. The
- ASCII code set uses the value 49 to represent the character '1'.
- If you look at the value of _number as an integer, it will be 49,
- although as a character it will be '1'. Example:
- cout << "char = " << _number << ", value = " << (int)_number << endl;
- This should print
- char = 1, value = 49
- (Assuming your system uses ASCII, which is usually the case.)
-
- If the input contains a sequence of characters you want to treat as an
- integer, read the value into a variable of some integer, not char, type:
- int i;
- unsigned long j;
- istrm >> i >> j;
-
- By the way, I suggest not using leading underscores on indentifiers in
- your own code. It is a dangerous habit, as they may conflict with things
- in system headers.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-