LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-05-2010, 12:22 PM   #1
drauk
Member
 
Registered: Sep 2007
Location: /MX/BC/TJ
Distribution: Slackware 13
Posts: 37

Rep: Reputation: 15
hexadecimal to double


Hi all
Someone know how i can convert from hex string to double in c/c++??

Example 40668472B020C49C is 180.139

reference page:
http://babbage.cs.qc.edu/IEEE-754/64bit.html

Thanks.
 
Old 04-05-2010, 12:33 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by drauk View Post
Hi all
Someone know how i can convert from hex string to double in c/c++??

Example 40668472B020C49C is 180.139

reference page:
http://babbage.cs.qc.edu/IEEE-754/64bit.html

Thanks.
Start looking for answers here: http://en.wikipedia.org/wiki/Floating_point .
 
Old 04-05-2010, 01:33 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by drauk View Post
Someone know how i can convert from hex string to double in c/c++??
Code:
    // Allocate a double on the stack
    double result;
    // Allocate a byte pointer on the stack
    unsigned char *byte;
    // Allocate a byte array on the stack
    unsigned char array[8];

    // Get the address of the double
    byte = &result;

    // Convert the string to bytes and save the values in the array
    for (int i = 0; i < 8; i++)
    {
        // Your solution here
    }

    // Copy the byte array to the byte pointer
    memcpy(byte, array, 8);

    // Print out your double
    printf("%f\n", result);

Last edited by David1357; 04-05-2010 at 01:34 PM. Reason: Removed note
 
Old 04-05-2010, 01:58 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Code:
double *d;
uint_64_t p = 0x40668472B020C49C;
d = (double *)&p;
 
Old 04-05-2010, 02:02 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by smeezekitty View Post
Code:
double *d;
uint_64_t p = 0x40668472B020C49C;
d = (double *)&p;
Endianness ?
 
Old 04-05-2010, 02:10 PM   #6
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
I thought it was little endian.
 
Old 04-05-2010, 02:34 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by smeezekitty View Post
I thought it was little endian.
80 bits or 64 bits (the OP wants 64 bits, but how will your conversion work ?) ?
 
Old 04-06-2010, 09:59 AM   #8
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by smeezekitty View Post
Code:
uint_64_t p = 0x40668472B020C49C;
What is really busted about this is that the OP wanted to handle strings.
 
Old 04-06-2010, 10:38 AM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by David1357 View Post
What is really busted about this is that the OP wanted to handle strings.
The string issue can be dealt with by 'atol' and friends. But the point is to understand FP number internal structure - I guess.
 
Old 04-07-2010, 08:47 AM   #10
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Sergei Steshenko View Post
The string issue can be dealt with by 'atol' and friends.
You know what? I had a major brain fade and forgot about strtoull!

Oh well, it looks a lot like a homework problem, and I didn't want to give too much help. Looks like I exceeded my goal.

Last edited by David1357; 04-07-2010 at 01:25 PM. Reason: Change "to" to "too".
 
Old 04-07-2010, 11:43 AM   #11
drauk
Member
 
Registered: Sep 2007
Location: /MX/BC/TJ
Distribution: Slackware 13
Posts: 37

Original Poster
Rep: Reputation: 15
Thanks all for your help, is not a homework i'm trying to read/understand the FLV header, and almost all the metadata values are double
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert string to hexadecimal kpachopoulos Programming 3 12-11-2007 06:23 AM
hexadecimal to denary using bc dazdaz Linux - Software 1 08-04-2007 01:43 PM
Ascii to hexadecimal values oulevon Programming 2 02-25-2006 09:08 PM
Hexadecimal Error moeFEAR Mandriva 1 10-21-2005 11:14 AM
Hexadecimal Error moeFEAR Linux - Laptop and Netbook 1 10-20-2005 09:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration