byte problem

This is a discussion on byte problem within the C++ Programming forums, part of the General Programming Boards category; ok say in my program i make a double variable for example. Somewhere in memory is my double, and it ...

  1. #1
    Registered User cppdude's Avatar
    Join Date
    Jan 2002
    Posts
    62

    byte problem

    ok say in my program i make a double variable for example. Somewhere in memory is my double, and it consists fundamentally of bytes in RAM. I would like to know how to get an array of bytes consisting of hte bytes that make up my double in RAM.

    thanks for any help

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    49
    Code:
    double d = 4.2;
    unsigned char *pByte = (unsigned char*)(&d);
    
    for(int i=0; i<sizeof(double)/sizeof(unsigned char); i++)
        cout << pByte[i];
    ...
    pByte is your needed;
    Hello, everyone.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    49
    Alternately (Is there any spell error with this word?):

    Code:
    union Dbl_Byte
    {
        double d;
        unsigned char c[sizeof(double)];
    };
    
    double YourDouble;
    
    Dbl_Byte u;
    u.d = YourDouble;
    ...
    then, u.c is your needed.
    Hello, everyone.

  4. #4
    Unregistered
    Guest
    are you looking for a bitfield reprsentation of your double?

    int num = 7;
    char bit[9];
    bit[0] = 0;
    bit[1] = 0;
    bit[2] = 0;
    bit[3] = 0;
    bit[4] = 0;
    bit[5] = 1;
    bit[6] = 1;
    bit[7] = 1;
    bit[8] = '\0';

    cout << "bitfield of 7 in most significant bit to least significant bit format = " << bit;

  5. #5
    Registered User cppdude's Avatar
    Join Date
    Jan 2002
    Posts
    62
    thanks hotman that union is what i need, but shouldnt hte size of the char string be sizeof(double) + 1 to account for the terminating char?

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    49
    It is an array of char, not a string. It need not NULL char to make end. And, maybe one of it's bytes is NULL.
    Hello, everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need loop to copy chunks from byte array
    By jjamjatra in forum C# Programming
    Replies: 2
    Last Post: 03-05-2009, 04:42 AM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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