如何显示大于127的十进制值的ASCII字符值。 [英] How to display ASCII char value for decimal values greater than 127.

查看:771
本文介绍了如何显示大于127的十进制值的ASCII字符值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码来使用十进制数字的char值。



I have tried the following code for using char value of decimal numbers.

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



在这种情况下ch显示k的'a'值(即正确)但是当我输入大于127的k值时,它不显示预期值。它显示了一些其他值。可能是由于非ASCII码(128-255)。

请帮我解决这个问题。


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.

推荐答案

int 大于127到 char ,结果不是预期的结果,因为该值被截断。 char 是一个签名类型,可以保存-128到127之间的值(另请参阅MSDN [ ^ ]。



根据您的代码,您可以使用负 int s或 unsigned char 非ASCII 8位字符的类型。
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.


我猜你的问题可能是你使用的输出(控制台或其他什么没有'有正确的代码页)。我个人最喜欢的代码页来查看二进制内容(将所有256个字符显示为可区分的字形)一直是Codepage 437 [^](在我的旧Hercules显示器的字符模式下喜欢它,只能显示cp437)。在代码页437上,只有chr(0),chr(32)和chr(255)具有相同的字形(空白)。所有其他角色都很容易区分。终端的许多默认代码页/字符集没有非ascii字符(> 127)的字形,因此它们显示占位符/默认字符,如空格,'?'或'。'而不是那些字符。了解如何调整运行程序的控制台。不幸的是,根据您使用的平台和控制台,您可能无法从您的程序中影响它。
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.


这篇关于如何显示大于127的十进制值的ASCII字符值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆