21xrx.com
2024-09-20 01:04:35 Friday
登录
文章检索 我的文章 写文章
C++ ASCII码输出程序
2023-06-29 06:41:48 深夜i     --     --
C++ ASCII码 输出 程序

C++是一种通用的高级编程语言,可以用来解决各种各样的问题。其中一项常见的任务是输出ASCII码,也就是美国信息交换标准代码。ASCII码是一个字节编码,每个字节的值表示一个字符。

以下是一个简单的C++程序,用于输出ASCII码的值:


#include <iostream>

using namespace std;

int main() {

  for(int i=0; i<=127; i++) {

   cout << i << "\t" << (char)i << endl;

  }

  return 0;

}

这个程序使用了一个for循环,从0到127遍历每个ASCII码值,并将其打印到屏幕上。对于每个ASCII码,程序还输出了它所代表的字符。其中"(char)i"将整数i转换为一个字符。

程序输出结果如下:


0    (null)

1    (start of heading)

2    (start of text)

3    (end of text)

4    (end of transmission)

5    (enquiry)

6    (acknowledge)

7    (bell)

8    (backspace)

9    (horizontal tab)

10   (line feed)

11   (vertical tab)

12   (form feed)

13   (carriage return)

14   (shift out)

15   (shift in)

16   (data link escape)

17   (device control 1)

18   (device control 2)

19   (device control 3)

20   (device control 4)

21   (negative acknowledge)

22   (synchronous idle)

23   (end of transmission block)

24   (cancel)

25   (end of medium)

26   (substitute)

27   (escape)

28   (file separator)

29   (group separator)

30   (record separator)

31   (unit separator)

32

33   !

34   "

35   #

36   $

37   %

38   &

39   '

40   (

41   )

42   *

43   +

44   ,

45   -

46   .

47   /

48   0

49   1

50   2

51   3

52   4

53   5

54   6

55   7

56   8

57   9

58   :

59   ;

60   <

61   =

62   >

63   ?

64   @

65   A

66   B

67   C

68   D

69   E

70   F

71   G

72   H

73   I

74   J

75   K

76   L

77   M

78   N

79   O

80   P

81   Q

82   R

83   S

84   T

85   U

86   V

87   W

88   X

89   Y

90   Z

91   [

92   \

93   ]

94   ^

95   _

96   `

97   a

98   b

99   c

100   d

101   e

102   f

103   g

104   h

105   i

106   j

107   k

108   l

109   m

110   n

111   o

112   p

113   q

114   r

115   s

116   t

117   u

118   v

119   w

120   x

121   y

122   z

123  

124   |

125   |

125   }

126   ~

127   (delete)

经过运行,可以发现该程序每一行都输出了一个ASCII码和相应的字符(以制表符分隔)。这个程序可以用于学习ASCII码及其在C++中的处理方式,并可以扩展以涵盖其他编码集。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复