学无先后,达者为师

网站首页 编程语言 正文

C++中使用cout以hex格式输出方式_C 语言

作者:qq_36208201   更新时间: 2022-12-09 编程语言

使用cout以hex格式输出

cout << "0x"<< hex << setiosflags(ios::uppercase) << setfill('0') << setw(2) << (int)10 << endl;

其中hex设置以16进制输出

  • setiosflags(ios::uppercase) 设置16进制数大写输出

setiosflags各参数定义

  • setiosflags(ios::fixed) 固定的浮点显示 
  • setiosflags(ios::scientific) 指数表示 
  • setiosflags(ios::left) 左对齐 
  • setiosflags(ios::right) 右对齐 
  • setiosflags(ios::skipws 忽略前导空白 
  • setiosflags(ios::uppercase) 16进制数大写输出 
  • setiosflags(ios::lowercase) 16进制小写输出 
  • setiosflags(ios::showpoint) 强制显示小数点 
  • setiosflags(ios::showpos) 强制显示符号 
  • setfill('0') 设置其他字符填充 如果输出字符的宽度不够 则以设置的字符输出

setw(2)设置输出宽度,如果宽度设置为3 则输出0x00A

PS:

最后的强转int:有资料说明cout << hex 只对整数有效 但是我在VS上不对数值进行强转也能以16进制输出

C++ cout的一些格式化输出

#include <iostream>
#include <iomanip>
 
using std::cout;
using std::endl;
 
int main(int argc,char *argv[],char *envp[])
{
    cout<<1234567890<<endl;
    cout<<std::setiosflags(std::ios_base::right)<<std::setw(20)<<std::setfill(' ')<<1234567890<<endl;
    cout.imbue(std::locale("english"));
    cout<<1234567890<<endl;
    cout.unsetf(cout.flags());
 
    cout<<std::showbase<<std::hex<<1234567890<<endl;
    cout.unsetf(cout.flags());
    return 0;
}

原文链接:https://blog.csdn.net/qq_36208201/article/details/124023712

栏目分类
最近更新