Thread: int ia[2] -> Double

  1. #1
    Unregistered
    Guest

    Question int ia[2] -> Double

    I would like to place 2 UINT32's into a Double:

    double d = 0;
    unsigned int ia[2] = {0xdeadbeef, 0xcafefeed};

    d = (double) ( (double)ia[0] | ((double)ia[1]<<32) );

    However, this does not compile:

    error C2296: '<<' : illegal, left operand has type 'double'

    My input is a byte or integer stream. My output has to be a double.

    How can I accomplish this?

    J0xEFF

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try
    Code:
    union {
      double d;
      unsigned int ia[2];
    } foo;
    
    foo.ia[0] = 0xdeadbeef;
    foo.ia[1] = 0xcafefeed;
    cout << foo.d;
    But you need to watch out for byte ordering

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One way is (this will give you a pointer to a double):
    Code:
    double *d;
    unsigned int ia[2] = {0xdeadbeef, 0xcafefeed};
    
    d = (double *) ia;
    cout << "d:" << *d << endl;
    Or another way is:
    Code:
    double *p;
    double d = 0;
    unsigned int ia[2] = {0xdeadbeef, 0xcafefeed};
    
    p = (double *) ia;
    d = *p;
    cout << "d:" << d << endl;

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    What about 0xBADF00D?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >What about 0xBADF00D?

    hehe, good one !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM