Solved

Extends characters to ascii code ???

Posted on 2001-08-14
18
255 Views
Last Modified: 2008-03-17

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
Comment
Question by:romilson
  • 5
  • 4
  • 2
  • +4
18 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6384225
Are you sure about your ASCII codes?

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

That's what your code produces...
0
 

Author Comment

by:romilson
ID: 6384257
Correction:

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

'C' ( "cecedil" :-)  character 128 of ascii table )
0
 
LVL 3

Expert Comment

by:cwrea
ID: 6384303
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
 

Author Comment

by:romilson
ID: 6384341
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
 
LVL 22

Expert Comment

by:nietod
ID: 6384677
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
 

Author Comment

by:romilson
ID: 6385042
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
 
LVL 22

Expert Comment

by:nietod
ID: 6385310
>> // 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
 
LVL 3

Expert Comment

by:cwrea
ID: 6385650
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
 
LVL 86

Expert Comment

by:jkr
ID: 6388828
BTW - I almost forgot about

#include <ctype.h>

int nASCII = __toascii('a');
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:romilson
ID: 6397031
jkr,

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

0
 
LVL 3

Expert Comment

by:cwrea
ID: 6397798
romilson, have you tried the suggested solutions?

0
 

Author Comment

by:romilson
ID: 6398664
yes .. but no one has resolved.

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

Romilson
0
 
LVL 3

Expert Comment

by:cwrea
ID: 6398692
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
 
LVL 11

Expert Comment

by:griessh
ID: 6851441
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
 
LVL 5

Expert Comment

by:Netminder
ID: 6874993
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6877288
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
 
LVL 11

Accepted Solution

by:
griessh earned 0 total points
ID: 6877392
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
 
LVL 5

Expert Comment

by:Netminder
ID: 6899755
Admin notified of User neglect. Points NOT refunded and question closed by
Netminder
CS Moderator
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now