Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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


C#
public string Converts(double d)
        {
            long ival = BitConverter.DoubleToInt64Bits(d);
            string hex = ival.ToString("X");
            return hex;

        }
Posted

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 type double is 64-bit, it cannot be fit in 32 bits:
http://en.wikipedia.org/wiki/IEEE_floating_point[^].

[EDIT]

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:

C#
internal static string ToHex(float value) {
    byte[] bytes = BitConverter.GetBytes(value);
    uint intValue = BitConverter.ToUInt32(bytes, 0);
    return intValue.ToString("X");
} //ToHex



—SA
 
Share this answer
 
v2
Comments
Amisha Makkar 1-Feb-13 2:06am    
u are right, then help me how can i convert float value into 32 bit hexadecimal?
Sergey Alexandrovich Kryukov 1-Feb-13 2:22am    
Oh, that's different story. Your original code is correct! Just replace 64 with 32 (see BitConverter methods for detail) and double with float. It should work right away.

Also, you can try to type cast double to float. Of course, it will be only successful with some valid subset of double values. And represent this float value as a string, as I mentioned above...

And, when (and if) you see it makes sense, please accept the answer formally (green button)...
—SA
Amisha Makkar 1-Feb-13 2:24am    
there is no such bitconverter method.tell me some other solution if you know.
Sergey Alexandrovich Kryukov 1-Feb-13 13:20pm    
Ah, OK, please see my updated answer, after [EDIT]. By the way, this way of conversion is more general, you rather want to prefer it.

When you see it makes sense, please accept the answer formally (green button) — thanks.
—SA
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;
}
 
Share this answer
 
v2
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Feb-13 1:58am    
Sorry, my 1. Isn't it obvious that it cannot work on double, because double is 64-bit?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900