如何将double转换为32位十六进制 [英] how to convert double into 32 bit hexadecimal
本文介绍了如何将double转换为32位十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
函数在c#中将double值转换为32位十六进制?
目前我可以将double转换为64位十六进制但我希望得到32位十六进制的结果..
以下代码用于将double转换为64位十六进制
function in c# to convert double value into 32 hexadecimal?
currently i am able to convert double into 64 bit hexadecimal but i want result in 32 bit hexadecimal..
following code i am using to convert double into 64 bit hexadecimal
public string Converts(double d)
{
long ival = BitConverter.DoubleToInt64Bits(d);
string hex = ival.ToString("X");
return hex;
}
推荐答案
这个问题毫无意义,因为数字不能是十六进制或十进制 ,只有数字的字符串表示可以。但是一个字符串不能是64位。
您的代码示例基本上是正确的,但您无法将其转换为返回int32的代码,非常原因很简单:类型double
是64位,它不能适合32位:
http://en.wikipedia.org/wiki/IEEE_floating_point [ ^ ]。
你不需要特别的转换。一般情况下的转换是字节数组和字节数组。这是本课程的主要目的。在您的情况下:
The question makes no sense, because a number cannot be "hexadecimal" or "decimal", only a string representation of a number can be. But a string cannot be "64-bit".
Your code sample is basically correct, but you cannot "convert" it to the code returning int32, by a very simple reason: the typedouble
is 64-bit, it cannot be fit in 32 bits:
http://en.wikipedia.org/wiki/IEEE_floating_point[^].
You don''t need a special conversion. General-case conversions are to array of bytes and from array of bytes. This is a main purpose of this class. In your case:
internal static string ToHex(float value) {
byte[] bytes = BitConverter.GetBytes(value);
uint intValue = BitConverter.ToUInt32(bytes, 0);
return intValue.ToString("X");
} //ToHex
非常感谢大家帮助我..
我的问题通过以下代码解决。
公共字符串转换(双d)
{
float f =(float)d;
byte [] convertToBytes = new byte [32];
string hex =;
convertToBytes = BitConverter.GetBytes(f);
for(int i =(convertToBytes.Count(x => x!= 0)) - 1; i> = 0; i--)
{
hex = hex +,+ convertToBytes [i] .ToString(X);
}
返回十六进制;
}
thanks a lot to all for helping me..
my problem get solved by following code.
public string Converts(double d)
{
float f = (float)d;
byte[] convertToBytes= new byte[32];
string hex = "";
convertToBytes = BitConverter.GetBytes(f);
for (int i = (convertToBytes.Count(x => x != 0))-1; i >=0; i--)
{
hex = hex + ","+ convertToBytes[i].ToString("X");
}
return hex;
}
http://msdn.microsoft.com/en-us/library/ms131023.aspx [<一个href =http://msdn.microsoft.com/en-us/library/ms131023.aspxtarget =_ blanktitle =新窗口> ^ ]
这篇关于如何将double转换为32位十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文