Solved

Convert hex string to double

Posted on 2007-11-27
8
3,836 Views
Last Modified: 2010-08-05
double xx=0;
string txt=null;

txt = "0942afc8131d6837";

xx=Double.Parse(txt, System.Globalization.NumberStyles.HexNumber);

this complies fine but when I run the application I get this error "An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll"

Is there a function in C# 2005 that will take a string Hex number and convert it to a double?
0
Comment
Question by:lcrogers
  • 4
  • 2
  • 2
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20361683
there is not, but you can build one by your own:

double hex2double(string hex)
{
        double result = 0.0;
        foreach (char c in hex)
        {
              double val = (double)System.Convert.ToInt32(c.ToString(),16);
              result = result * 16.0 + val;
        }
        return result;
}
0
 
LVL 6

Accepted Solution

by:
Roopesh_7 earned 250 total points
ID: 20361764
Hi friend,
Use below method to convert

double xx = 0;
            string txt = null;
           
            txt = "0942afc8131d6837";

            xx = Convert.ToInt64(txt, 16);
            Response.Write(xx);

0
 

Author Comment

by:lcrogers
ID: 20361813
I need it to be a doule precession number,  this will give me a whole number.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20361951
what do you mean with a double precision number? both examples comply with your question.
0
 
LVL 6

Expert Comment

by:Roopesh_7
ID: 20362008
 string txt = null;
           
            txt = "0942afc8131d6837";

            txt = Convert.ToInt64(txt, 16).ToString("#0.00");

give a string format at the end......

then convert that string to double by convert.todouble(txt );
0
 

Author Comment

by:lcrogers
ID: 20362123
it must be converted to a negetive earth center to earth fix number (exsample: -53512345.3567N, -36512342.3345E;  I am getting these values from a military GPS grid system.

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20362412
you have to do it with unsafe code. Assuming both coordinates are inside a single hex:

        unsafe public static void Transform(string hex, out float x, out float y)
        {
            UInt32 x0 = System.Convert.ToUInt32(hex.Substring(0, 8), 16);   // first half of hex code
            UInt32 y0 = System.Convert.ToUInt32(hex.Substring(8, 8), 16);   // last half of hex code

            x = *((float *)&x0);
            y = *((float *)&y0);
        }

Assumming it is just one component:
        unsafe public static void Transform(string hex, out double a)
        {
            UInt64 a0 = System.Convert.ToUInt64(hex16);
            a = *((double *)&a0);
        }


but with the sample hex string you have provided, it doesn't output any valid value as you shown.
Also, you have to take care about the byte ordering (big/little endian)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20362416
also you will need to instruct the compiler to allow the unsafe code in the Project Settings.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reset asp.net login password 4 42
What namespace do I need to import? 2 29
Service Controller 1 23
Calculate number of nights between two dates 5 25
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question