C++中输入输出的十六进制八进制

[来源] 达内    [编辑] 达内   [时间]2012-09-19

默认状态下,数据按十进制输入输出。如果要求按八进制或十六进制输入输出,在cin或cout中必须指明相应的数据形式,oct为八进制,hex为十六进制,dec为十进制

1、数的进制

默认进制:
默认状态下,数据按十进制输入输出。如果要求按八进制或十六进制输入输出,在cin或cout中必须指明相应的数据形式,oct为八进制,hex为十六进制,dec为十进制。

< div style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 0px; color: rgb(153, 153, 153); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
 1

 int i, j, k, l; 
 2

 cout<<”Input i(oct), j(hex), k(hex), l(dec):”<<endl;  3
 cin>>oct>>i; //输入为八进制数

 4 cin>>hex>>j; //
输入为十六进制数

 5 cin>>k; //
输入仍为十六进制数

 6 cin>>dec>>l; //
输入为十进制数

 7 cout<<”hex:”<<”i=”<<hex<<i<<
endl;  8
 cout<<”dec:”<<”j=”<<dec<<j<<′\t′<<”k=”<<k<<endl; 
 9

 cout<<”oct:”<<”l=”<<oct<<l; 10
 cout<<dec<<endl; //恢复十进制输出状态

11 
12 【执行结果】: 
131


)输出提示:Input i(oct), j(hex), k(hex), l(dec): 142
)此时从键盘输入: 032 0x3f
 0xa0 17
 <CR>
153
)输出结果为: 16
 
17 hex:i=1a 
18 dec:j=63
 k=16019
 oct:l=21
< p style="margin: 5px auto; text-indent: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">几点说明:

使用不带.h的头文件<iostream>时,必须在cin中指明数制,否则从键盘输入时,不认八进制和十六进制数开头的0和0x标志。指明后可省略0和0x标志。

进制控制只适用于整型变量,不适用于实型和字符型变量。

输入数据的格式、个数和类型必须与cin中的变量一一对应,否则不仅使输入数据错误,而且影响后面其他数据的正确输入。

在cin或cout中指明数制后,该数制将一直有效,直到重新指明使用其他数制。

2、数据间隔

常用设置方法:输出空格符或回车换行符。

指定数据输出宽度:用C++提供的函数setw()指定输出数据项的宽度。setw()括号中通常给出一个正整数值,用于限定紧跟其后的一个数据项的输出宽度。如:setw(8)表示紧跟其后的数据项的输出占8个字符宽度。

< div style="background-color: rgb(245, 245, 245); font-family: 'Courier New'; font-size: 12px; border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 0px; color: rgb(153, 153, 153); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; " class="cnblogs_code">
 1

 int i=2
, j=3

;  2
 float x=2.6
, y=1.8

;  3
 cout<<setw(6)<<i<<setw(10
)<<j<<endl; 

 4 cout<<setw(10
)<<i*j<<endl;  5
 cout<<setw(8

)<<x<<setw(8)<<y<<endl; 
 6
 
 7 则输出结果为: 
 8 2
 3 9
 6

10 2.6
 1.8
< p style="margin: 5px auto; text-indent: 0px; font-family: verdana, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">setw()只能限定紧随其后的一个数据项,输出后即回到默认输出方式。
使用setw()必须在程序开头再增加一句: #include<iomanip>

资源下载