• (转载)C++ cout 输出 16, 8 , 2进制


    (转载)
    #include <iostream>
    #include <iomanip>
    #include <bitset>
    using std::bitset;
    using std::hex;
    using std::oct;
    using std::cout;
    using std::cin;
    using std::endl;
    int main()
    {
     int a=10; 
     cout<<"Dec:"<<a<<endl; 
     cout<<hex<<"Hex:"<<a<<endl;
     cout<<oct<<"Oct:"<<a<<endl;
     cout<<bitset<32>(a)<<endl;
     getchar();
     return 0;
    }
    
     
    
     
    
    C++ 操作符
    
     
    
    注:下面的scientific 和 fixed不能同时使用
    double a=123.456789012345;对a赋初值
    (1) cout<<a;输出: 123.456  
    (2) cout<<setprecision(9)<<a;输出: 123.456789  
    (3) cout<<setprecision(6);恢复默认格式(精度为6)
    (4) cout<< setiosflags(ios∷fixed);输出: 123.456789
    (5) cout<<setiosflags(ios∷fixed)<<setprecision(8)<<a;输出: 123.45678901
    (6) cout<<setiosflags(ios∷scientific)<<a;输出: 1.234568e+02
    (7) cout<<setiosflags(ios∷scientific)<<setprecision(4)<<a; 输出: 1.2346e02
    下面是整数输出的例子: 
    int b=123456;对b赋初值
    (1) cout<<b;输出: 123456
    (2) cout<<hex<<b; 输出: 1e240     
    (3) cout<<setiosflags(ios∷uppercase)<<b;输出: 1E240     
    (4) cout<<setw(10)<<b<<′,′<<b; 输出:     123456123456
    (5) cout<<setfill(′*′)<<setw(10)<<b;输出: **** 123456
    (6) cout<<setiosflags(ios∷showpos)<<b;输出: +123456
    #include <iostream>
    #include <bitset>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int a = 10;
        bitset<32> b0(a);
        // 输出二进制
        cout << b0 << endl;
    
        // 转化string类型
        bitset<32> b1(7);
        string str(b1.to_string());
        cout << str << endl;
    
        return 0;
    }
  • 相关阅读:
    Eclipse 3.7 安装Maven插件时报错:requires 'bundle org.slf4j.api 1.6.2' but it could not be found
    Windows下IIS+PHP5.3.x ZendGuardLoader的配置方法
    IIS7.x运行PHP
    顺手的Linux发行版及其工具推荐
    nc 简单的使用
    nginx日志简单分析工具
    批量转换cue文件编码
    Linux Tweak:交换 Caps_Lock 与 Control_R
    Word 2013测试
    start running 开始跑步减肥
  • 原文地址:https://www.cnblogs.com/Robotke1/p/3063295.html
Copyright © 2020-2023  润新知