Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried the following code for using char value of decimal numbers.

C++
int k = 97;
char ch = static_cast<char>(k);

Here in this case ch shows 'a' value(i.e. correct) for k but when I enter value of k greater than 127, it is not displaying expected value. It is showing some other values. May be it is due Non ASCII code (128-255).
Please help me out in this problem.
Posted
Updated 11-Jun-13 5:39am
v2
Comments
nv3 11-Jun-13 11:56am    
Please add your platform and the way you are outputting this character.
lewax00 11-Jun-13 17:35pm    
Technically speaking, there are no ASCII characters above 127. ASCII is a 7-bit encoding, only capable of representing 0-127, anything higher requires an eighth bit, which makes it extended ASCII, and is not standard across platforms.

When casting an int greater than 127 to a char, the result is not the expected one because the value is truncated. char is a signed type which can hold values from -128 to 127 (see also MSDN[^]).

Depending on your code you may use negative ints or the unsigned char type for non ASCII 8-bit characters.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jun-13 13:11pm    
Exactly, my 5.
Besides, who needs these so called "extended ASCII" characters or one of those mutually-exclusive encodings using this range? It could be used only for solving some problem with the compatibility with really obsolete legacy. The character which used to be placed in this range are now standardized by Unicode with some different code points.
—SA
I guess your problem can be that the output you use (console or whatever doesn't have the right codepage). Personally my favorite codepage to view binary stuff (to display all 256 characters as distinguishable glyphs) has always been Codepage 437[^] (loved it on the character mode of my old Hercules monitor that could display only cp437). On codepage 437 only chr(0), chr(32) and chr(255) have the same glyph (blank). All other characters are easily distinguishable. Many default codepages/character sets for terminals have no glyphs for non-ascii characters (>127) so they display a placeholder/default char like space, '?' or '.' instead of those chars. Find out how to tweak the console in which you run the program. Unfortunately you may not be able to influence it from your program depending on the platform and console you are using.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900