您好~我想实现浮点型数据转换为unsigned char的过程 ,但是按图这样子操作时,发现出来的结果C是42
,但是我想将大于255的值都强转为255,应该怎么写啊???
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
你好,
C语言中char类型可表示的数最大就是255,对于大于这个数的值强转成char,C语言采用的方法是用这个数模256,也就是c=a&0xff,
C语言本身没有你想要实现的取饱和的功能,如果你要实现这样的代码,可以通过:
c = a > 255 ? 255: ((unsigned char)a)
这样的条件运算符里完成