如何通过浮浮代表转换为uint? [英] How to convert float to uint by float representation?

查看:183
本文介绍了如何通过浮浮代表转换为uint?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 我会做这一些浮点表示转换为DWORD。乘坐从地址的价值和铸内容DWORD

In C I will do this to convert float representation of number into DWORD. Take the value from the address and cast the content to DWORD.

dwordVal = *(DWORD*)&floatVal;



因此​​,例如 44.54321 将成为 0x42322C3F

我怎样才能做到在 C#

推荐答案

您可以使用的 BitConverter 类:

You can use the BitConverter class:

uint value = BitConverter.ToUInt32(BitConverter.GetBytes(44.54321F), 0);
Console.WriteLine("{0:x}", value); // 42322c3f

您也可以做到这一点更直接使用的 不安全背景

You could also do this more directly using an unsafe context:

float floatVal = 44.54321F;
uint value;
unsafe { 
    value = *((uint*)(&floatVal));
}
Console.WriteLine("{0:x}", value); // 42322c3f



不过,我强烈建议避免这一点。请参见如果您在C#中使用指针(不安全的代码)?

这篇关于如何通过浮浮代表转换为uint?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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