将uint64转换为uint32 [英] convert uint64 to uint32

查看:2589
本文介绍了将uint64转换为uint32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个uint64变量,但我想传递的函数只接受uint32。

I have a uint64 variable ...but the function in which I want to pass it only accepts uint32.

我尝试使用 static unsigned int ToUInt32 (uint64 variable); 引用MSDN,但它给出一个错误,精度丢失。

I tried using static unsigned int ToUInt32(uint64 variable); reference MSDN, but it gives an error that precision lost.

有没有办法将uint64转换为uint32而不丢失任何数据?

Is there any way to convert uint64 to uint32 without losing any data?

推荐答案

仔细想想你要做什么。你正在采取一个数字,可能会占用64位,然后切成两半。
当然,在这个实例中你将丢失数据。

Well think about what you are trying to do. You are taking a number which will potentially take up to 64bits and then cutting in half. Of course you are going to lose data in this instance.

如果你确定给定的数字不会大于uint32,那么你可以试试这个

If you are sure that the number being given is not going to be larger than uint32 then you can try this

uint64 largeNumber = 9876543210;
uint32 smallNumber = largeNumber & 0xFFFFFFFF;

但是如果它大于32位,这将会失去你的一半数字。

But this WILL lose you half of your number if it's bigger than 32bits.

这篇关于将uint64转换为uint32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆