• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

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
0
romilson
Asked:
romilson
  • 5
  • 4
  • 2
  • +4
1 Solution
 
jkrCommented:
Are you sure about your ASCII codes?

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

That's what your code produces...
0
 
romilsonAuthor Commented:
Correction:

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

'C' ( "cecedil" :-)  character 128 of ascii table )
0
 
cwreaCommented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
romilsonAuthor Commented:
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
0
 
nietodCommented:
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.
0
 
romilsonAuthor Commented:
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
0
 
nietodCommented:
>> // 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.
0
 
cwreaCommented:
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.
0
 
jkrCommented:
BTW - I almost forgot about

#include <ctype.h>

int nASCII = __toascii('a');
0
 
romilsonAuthor Commented:
jkr,

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

0
 
cwreaCommented:
romilson, have you tried the suggested solutions?

0
 
romilsonAuthor Commented:
yes .. but no one has resolved.

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

Romilson
0
 
cwreaCommented:
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.

0
 
griesshCommented:
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
0
 
NetminderCommented:
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.

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

Netminder
Community Support Moderator
Experts Exchange
0
 
MoondancerCommented:
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
0
 
griesshCommented:
Moondancer

I think the delete was done because of my comment. As I stated I don't think any of the above comments provide a clean solution for romilson's problem. Nevertheless there is enough information for a PAQ at 0 points ...

======
Werner
0
 
NetminderCommented:
Admin notified of User neglect. Points NOT refunded and question closed by
Netminder
CS Moderator
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now