 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
TK Guest
|
Posted: Sat Feb 28, 2004 9:24 pm Post subject: 7 bit char |
|
|
I have encountered a very strange bug. I am using a variable defined
as:
char ch;
If I assigned a character like 'Ö' (ascii 153) with a statement like:
ch = 'Ö';
It gets the value -103. It's as if char now is a signed datatype,
which it has never been before. I am developing in MS DevStudio 6.0.
Is there a setting somewhere that could have been changed? I have
never seen this before, and I have coded for about 10 years. I have
even run the code I have problems with now before without this problem
(most recently just a few days ago). Can anything else I have
installed screwed up this? When researching the problem on the web, I
did find some references to a file called limits.h that deals with
this, and it does exist on my computer, but I have not added any
referencees to it my code.
Any help is much appreciated,
Thanks,
TK
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sat Feb 28, 2004 9:35 pm Post subject: Re: 7 bit char |
|
|
* [email]t_karlsson72 (AT) hotmail (DOT) com[/email] (TK) schriebt:
| Quote: |
I have encountered a very strange bug. I am using a variable defined
as:
char ch;
If I assigned a character like 'Ö' (ascii 153) with a statement like:
|
Incorrect. The ASCII code does not define code points above 127. Also,
'Ö' is not an ASCII character.
Possibly you mean ISO 8859-1?
| Quote: | ch = 'Ö';
It gets the value -103. It's as if char now is a signed datatype,
|
Yep, and from 153 - 256 = -103 you can deduce that this implementation's
char type is most probably eight bits.
Hence the subject line about "7 bit char" is most probably incorrect,
and in any event misleading.
| Quote: | which it has never been before.
|
Incorrect. Neither C nor C++ has ever defined whether 'char' is signed
or unsigned. That's up to the implementation. On the other hand, 'signed
char' is signed, and 'unsigned char' is unsigned. Many compilers have some
means of forcing 'char' to be signed or unsigned.
| Quote: | I am developing in MS DevStudio 6.0.
Is there a setting somewhere that could have been changed? I have
never seen this before, and I have coded for about 10 years.
|
Incorrect. MSVC 6.0 isn't 10 years old. Also, the default in MSVC 6.0 was,
as I recall, that 'char' mapped to 'signed char'.
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Sun Feb 29, 2004 1:47 am Post subject: Re: 7 bit char |
|
|
TK wrote:
| Quote: | It's as if char now is a signed datatype,
which it has never been before. I am developing in MS DevStudio 6.0.
Is there a setting somewhere that could have been changed? I have
never seen this before, and I have coded for about 10 years.
|
Which compiler did you use before? As far as I remember, char has always
been a signed datatype by default on Microsoft compilers as well as many
other C/C++ compilers I happened to work with in the last 17 years.
BTW, you can tell MSVC to make char unsigned by using the /J option.
Regards,
Alberto Barbati
|
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Sun Feb 29, 2004 11:38 am Post subject: Re: 7 bit char |
|
|
"TK" <t_karlsson72 (AT) hotmail (DOT) com> wrote
| Quote: | ... It's as if char now is a signed datatype,
which it has never been before. I am developing in MS DevStudio 6.0.
Is there a setting somewhere that could have been changed?
|
Note that, according to the C++ standard, the integral type 'char'
may be either signed or unsigned.
Unlike other integral types (short, int, long), 'char', 'signed char'
and 'unsigned char' are tree distinct types.
Rather than changing compiler settings, it would be wiser to write
code that can work with both signed and unsigned variants of 'char':
replace 'char' with 'unsigned char', or cast values to 'unsigned char'
when needed.
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sun Feb 29, 2004 8:42 pm Post subject: Re: 7 bit char |
|
|
"TK" <t_karlsson72 (AT) hotmail (DOT) com> wrote
| Quote: | Re: 7 bit char
I have encountered a very strange bug. I am using a variable defined
as:
char ch;
If I assigned a character like 'Ö' (ascii 153) with a statement like:
ch = 'Ö';
|
1. Please explain how you can represent the number 153 with
only seven bits.
2. The ASCII character set only defines character encodings
with values from zero through 127.
| Quote: |
It gets the value -103.
|
You're seeing implementation-specific behavior.
| Quote: | It's as if char now is a signed datatype,
|
It can be, and often is.
| Quote: | which it has never been before.
|
The language does not specify whether type 'char' is signed
or unsigned. It must be one or the other, but which is
left to the implementation.
| Quote: | I am developing in MS DevStudio 6.0.
|
All Microsoft compilers I've ever used for the last two
decades or so, *all* have type 'char' which is signed.
(but all offered an 'option' to change it to unsigned
if desired).
| Quote: | Is there a setting somewhere that could have been changed?
|
Yes. See above.
| Quote: | I have
never seen this before, and I have coded for about 10 years.
|
In 10 years, you never noticed that MS compilers' type
'char' is signed by default?
| Quote: | I have
even run the code I have problems with now before without this problem
(most recently just a few days ago).
|
A common situation with C is the existence of 'problems'
often for a long time, that don't visibly manifest themselves.
| Quote: | Can anything else I have
installed screwed up this?
|
That's a possiblity.
| Quote: | When researching the problem on the web, I
did find some references to a file called limits.h that deals with
this,
|
The standard header <limits.h> defines the characteristics
of many language entities whose exact specifications are
left to the implementation (but subject to certain restrictions
and requirements). E.g. the macro 'CHAR_BIT' tells how many
bits comprise a character (the language mandates that this
value be at least eight, but allows it to be greater).
| Quote: | and it does exist on my computer, but I have not added any
referencees to it my code.
|
You don't need the header unless you need to refer to any
symbols it defines.
| Quote: |
Any help is much appreciated,
|
Try changing the default 'signedness' of char (see the documentation
to learn how), or change your type in your source code to 'unsigned char'
specifically.
In any event, is it really necessary to be concerned with the
actual numerical values of your characters? Why not specify them
with character (or string) literals, e.g. 'A', "ABC".
-Mike
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|