微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

'double' 到 'unsigned char' 的转换是如何工作的?

如何解决'double' 到 'unsigned char' 的转换是如何工作的?

我希望能够将 double 转换为 'unsigned char' 并且我似乎不明白为什么 'unsigned char' 结果出现在字符 '>' 中,即使转换完成(在函数内部)。谁能向我解释问题出在哪里以及如何解决?这将是一个很大的帮助。

 #include <iostream>
 using namespace std;

 unsigned char f_wd(double force1);

 int main() {
 double x = 512;

  int dog = f_wd(x);
  cout << dog << endl;            // this outputs 62 

  unsigned char cat = f_wd(x);
  cout << cat << endl;             // but this outputs '>'

}


 unsigned char f_wd(double force1){
 double animal = (124.0/1024)*force1;
unsigned char animal2 = (unsigned char)animal;
 return animal2;

 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。