Click here to Skip to main content
14,739,326 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
float input = 25;
            byte[] buffer = BitConverter.GetBytes(input);

buffer contains 0x00 0x00 0xc8 0x41.I need to convert it back to float i.e 25.I knows only hex values,Please tell how to do it.
Posted
Comments
ridoy 14-Aug-13 6:06am
   
If you want it in float then why you post question with a title of integer?!

What's wrong with BitConverter.ToSingle[^] method?

e.g.

float input = 25;
byte[] buffer = BitConverter.GetBytes(input);
float output = BitConverter.ToSingle(buffer,0);
   
Comments
   
Hi CPallini,
float output = BitConverter.ToSingle(buffer,0);
above line having compile time exception.
CPallini 14-Aug-13 6:40am
   
What is a 'compile time exception'? It compiles (and runs) fine, on my system.
   
This is the only correct answer, my 5.
Gosh, we have a lot of answer spam, including OP's.
—SA
CPallini 14-Aug-13 7:51am
   
Thank you.
   
Comments
   
Thanks for reply i have got this in internet.
private Single ConvertHexToSingle(string hexVal)
       {

           try
           {

               int i = 0, j = 0;

               byte[] bArray = new byte[4];

               for (i = 0; i <= hexVal.Length - 1; i += 2)
               {

                   bArray[j] = Byte.Parse(hexVal[i].ToString() + hexVal[i + 1].ToString(), System.Globalization.NumberStyles.HexNumber);

                   j += 1;

               }

               Array.Reverse(bArray);

               Single s = BitConverter.ToSingle(bArray, 0);

               return (s);

           }
           catch (Exception ex)
           {

               throw new FormatException("The supplied hex value is either empty or in an incorrect format.  Use the " +

                   "following format: 00000000", ex);

           }

       }
   
Comments
   
Gibberish. What a gibberish! Have some shame, how can you post it as "solution"? The only correct solution is Solution 3.
—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