Link to home
Start Free TrialLog in
Avatar of romilson
romilson

asked on

Extends characters to ascii code ???


I have made this:

int iAscCode;

iAscCode = 'Y';

cout << iAscCode << endl; // 121 - Right !

iAscCode = 'C';

cout << iAscCode << endl;  // -57  - Error! must be 128

-----------

here , the size of int is 4 bytes .. 32 bits. .

127  =  01111111
128  =  10000000

both occupies 1 byte , and int have 4 bytes.
the problem is not with int type. But ..

Where is the error ?
And .. How to resolve it ?

thanks a lot.

Romilson
Avatar of jkr
jkr
Flag of Germany image

Are you sure about your ASCII codes?

'C' --> 67 dec
'Y' --> 89 dec

That's what your code produces...
Avatar of romilson
romilson

ASKER

Correction:

'y' ( lower case )   character 121 of ascii table )

'C' ( "cecedil" :-)  character 128 of ascii table )
What characters are you assigning to iAscCode?  Your post indicates the first is "capital letter y" and the second is "capital letter c".  Those ASCII characters have values of 89 and 67, respectively.

However, I am guessing there was an error in transcription; perhaps you cut and paste extended characters and the message board translated them?  To obtain a value of 121, the first character would have to be "small letter y".

The second character is a little trickier.  It depends on what ASCII code page you use.  On Windows 2000 in a command prompt window, it shows what looks like "capital c with cedilla" whereas if I examine the output in Notepad (after redirection) it looks like the Euro symbol.

I don't think literal characters in that range aren't guaranteed to be treated properly by the compiler.  So if you want to specify a particular extended-range character in your source code, you should use the numeric value for any characters in the extended range (128-255).

Often it helps to have #define or enums for such characters that provide meaningful names. e.g:

#define EURO_SYMBOL  128
...
iAscCode = EURO_SYMBOL;
cout << iAscCode << endl;

Of course you still need to make sure that your program is running with the proper extended character set loaded in whatever environment you're in.
right !

I am brasilian , and my code page is different.

the first is a small letter "y"
the second is a "upper case" capital letter C with cedilla"

Romilson
Then you need to be using wide characters.

Like

iAscCode = L'C';

its also probably a good idea to use wchar_t instead of int as the data type for storing these values.
nietod,

#include <wchar.h>
#include <iostream.h>

void main () {

   wchar_t wcAscCode;

   /*below is not C. Is C with cedilla , but
   the forum don't show it*/

   wcAscCode = L'C';

   // print 199 - Error .. Must be 128
   // By the my ascii table
   cout << wcAscCode << endl;

}

Have I to create a table in memory ? ( i think not good )

Romilson
>> // print 199 - Error .. Must be 128
You are probably using unicode, not ASCII.  The ASCII character set only goes up to 127.  After that there are different unofficial ASCII character sets that go from 128 to 255.   You usually don't want to use these ase they are not consistent and tend not to be used internationalized code.  instead use uncode.  Your source code is probably being written in unicode, that is why that character is translated to the unicode value, not the extended ASCII value you want.
You should not rely on your compiler to properly treat string literals that contain extended characters.  As I suggested before, you should use the numeric value in a constant or #define, and use that symbolic constant wherever you want to output the character.
BTW - I almost forgot about

#include <ctype.h>

int nASCII = __toascii('a');
jkr,

__toascii dont treat extend characters..
but , thanks for.

romilson, have you tried the suggested solutions?

yes .. but no one has resolved.

I will do a table.txt , witch contains the extends codes

Romilson
I provided you with a solution that works.  If you include extended character literals in your C++ source code, it is implementation-defined [i.e. up to the people who wrote your compiler] as to what the result will be.  Consequently, without knowing exactly how your compiler works, if you want to guarantee you'll get out a certain value, use a symbolic constant to represent that value, rather than the character literal.

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to refund the points and PAQ at zero points since nobody had a satisfying answer for you.

I was tempted to suggest one of the comments as an answer, but from my personal experience working in the area of i18n I am sure there is a 'clean' way to deal with this problem. Unfortunatelly I have no experience at all in that respect under Windows, so I can't suggest a better solution.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
romilson,

Our records show you recently logged into EE. In addition to this question, you have the following open ones. Please resolve this one and the others within seven (7) days; inattention could result in action with regard to your account.

https://www.experts-exchange.com/questions/Q.20146197.html
https://www.experts-exchange.com/questions/Q.20199847.html
https://www.experts-exchange.com/questions/Q.20269127.html
https://www.experts-exchange.com/questions/Q.20271179.html
https://www.experts-exchange.com/questions/Q.20168254.html

Netminder
Community Support Moderator
Experts Exchange
This question has been pended for deletion, versus awarded.  Pending deletions will not clear.  Please comment here as to why you tried deleting this item so we can help you further.
Thanks,
Moondancer - EE Moderator
ASKER CERTIFIED SOLUTION
Avatar of griessh
griessh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Admin notified of User neglect. Points NOT refunded and question closed by
Netminder
CS Moderator