char c = -1;如果用语句:printf("%d",(unsigned char)c)将输出255若用cout怎么实现输出255?
若这样cout<<(unsigned int)c<<endl则输出一个很大的数,我只要输出255,咋整啊?

解决方案 »

  1.   


    int main(int argc, char* argv[])
    {
       char c = -1;
       cout<<int(byte(c))<<endl; //255
       getch();
       return 0;
    }
      

  2.   

    谢谢,不过我在VS2008里byte不是关键字哎,报错了
      

  3.   


    原来是VS
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <conio.h>
    #include <windows.h>// 注意添加 才能使用byte
    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
    {
    char c = -1;
    cout<<int(BYTE(c))<<endl; //255
    getch();
    return 0;
    }
      

  4.   

    非常感谢!
    我刚发现这样也可以cout<<int(unsigned char(c))<<endl;但是我不知道原因不理解。。
      

  5.   

    BYTE 和unsigned char表示范围都是0-255
    可以看作BYTE就是unsigned char  
    unsigned char是字符型
    VC里 Byte 是int型