C++ Converting a float to an unsigned char?

后端 未结 2 483
时光取名叫无心
时光取名叫无心 2021-02-10 21:24

I\'m new to C++, and doing a bit of googling I thought sprintf would do the job, but I get an error upon compiling that I can\'t convert between an unsigned c

2条回答
  •  粉色の甜心
    2021-02-10 21:57

    Not sure, what is your exact requirement [ since you didnot paste a snippet of the code .. as Greg requested ], following example might resolve it :

    #include 
    #include 
    using namespace std;
    
    int main()
    {
        float i=1;
        unsigned char c;
        c = static_cast(i);
        cout << c << endl;
        getch();
        return 0;
    }
    

提交回复
热议问题