creating a double precision value from individual bytes

This is a discussion on creating a double precision value from individual bytes within the C Programming forums, part of the General Programming Boards category; Hi, Could anybody tell me if they know of any tricks with pointers or any functions in standard libraries that ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    creating a double precision value from individual bytes

    Hi,

    Could anybody tell me if they know of any tricks with pointers or any functions in standard libraries that would allow me to do this:
    I am interfacing with a piece of hardware that returns the 64 bits of a double precision value as an array of 8 bytes. These correspond to the 64 bits used for a double precision number. Other than resorting to using the equation (such as in wikipedia) to generate this value, I was hoping I could directly allocate these bytes to a pointer to double but so far I haven't found a way to do this. Is it possible?

    Thanks,

    Gareth

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    So long as your 8 bytes are in the correct byte order for the endian of your machine, then you can do
    Code:
    memcpy( &myDouble, mybuffer, sizeof(myDouble) );
    But you're making a few assumptions by doing this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by garethv View Post
    ... I was hoping I could directly allocate these bytes to a pointer to double but so far I haven't found a way to do this. Is it possible?
    Yep, just point a double* to the base of the 8 byte char array and dereference it.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > Yep, just point a double* to the base of the 8 byte char array and dereference it.
    Better watch out for those alignment exceptions then, if such a pointer were to be mis-aligned for a double.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Salem View Post
    > Yep, just point a double* to the base of the 8 byte char array and dereference it.
    Better watch out for those alignment exceptions then, if such a pointer were to be mis-aligned for a double.
    Good point!

    @OP:
    Based on Salem's comment, explicitly specify a #pragma pack(8) directive to ensure proper alignment.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itCbitC View Post
    Good point!

    @OP:
    Based on Salem's comment, explicitly specify a #pragma pack(8) directive to ensure proper alignment.
    Which, as far as I know, only works on structs.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by CommonTater View Post
    Which, as far as I know, only works on structs.
    Hmm! you are right and I wonder WTH I was thinking of, albeit alignment and indirection can be eliminated by unionizing an 8 byte char array and a double.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    itCbitC: Ever get the feeling that you're digging a hole?
    "The hardware returns is as an array of 8 bytes". Either the hardware aligns it or it doesn't. It's not a case of trying to write code that makes sure it is already aligned. It is a case of ... just align the damm thing.

    The memcpy is probably the best option.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    2
    Thanks for all your help! The memcpy solution seems to work fine and is just what I was looking for. As this will only be used on Windows machines I guess I'm safe to assume the order won't change.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by garethv View Post
    Thanks for all your help! The memcpy solution seems to work fine and is just what I was looking for. As this will only be used on Windows machines I guess I'm safe to assume the order won't change.
    The only thing that's safe to assume is that "It works this time"... to get beyond that requires testing.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    If you want to be sure about the correct alignment, just use a union:

    Code:
    union {
        char from_hardware[sizeof(double)];
        double to_software;
    };
    It is better than the #pragma.
    I never put signature, but I decided to make an exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accesing individual bytes in an int
    By kkk in forum C Programming
    Replies: 16
    Last Post: 05-19-2011, 05:03 PM
  2. Manipulating individual bits in a double precision number
    By thetinman in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2010, 07:19 AM
  3. extended vs double precision
    By Lind in forum C Programming
    Replies: 2
    Last Post: 09-19-2007, 02:02 PM
  4. More precision than a double
    By manutd in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2006, 05:49 AM
  5. Reading Individual bits from 2 bytes
    By Giania in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 02:16 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21