在c ++中转换uchar

时间:2011-03-10 06:37:57

标签: c++

如何将uchar转换为int?

4 个答案:

答案 0 :(得分:5)

如果uchar的typedef为unsigned char,则很简单:

unsigned char c = 'A';
int i = c; //automatic conversion uchar into int!

施法(即int i = (int)c)过度。

答案 1 :(得分:1)

#include <iostream>

int main(int argc, char *argv[])
{
    unsigned char x = '\xff';
    std::cout << static_cast<int>(x) << std::endl;
    return 0;
}

输出:

255

答案 2 :(得分:0)

只需将其分配给int变量进行隐式转换

unsigned char c = 'c';
int i = c;

或明确地输入变量

unsigned char c = 'c';
int i = (int)c;

答案 3 :(得分:0)

尝试:

void func(int x)
{
}

int main()
{
    unsigned char c = 'c';
    int           t = c;   // auto converts to int

    func(c);   // Works the same (auto converts)
    func(t);   // Or use the t variable.
}