Thread: convert set of chars into double

  1. #1
    carpe diem
    Join Date
    Jan 2010
    Posts
    46

    convert set of chars into double

    Hi,
    I have 8 char stored in a char array.

    char char_buff[65536];

    char_buff[x]=68;
    char_buff[x+1]=59;
    ...
    char_buff[x+7]=-56;


    I would like to combine those 8 char into one double type variable. I thought about using bit manipulation and temporarily store all the char into a long int and then somehow convert that to a double.
    Can anyone please point me in the right direction?

  2. #2
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    -56 is not a valid ascii character code

    http://www.asciitable.com/

    look up strtod for double conversion
    Last edited by jimtuv; 06-14-2010 at 08:56 AM.

  3. #3
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    well for some reason when I store the binary numbers in my char array and do a "quickwatch" of the array index I get -27 which, according to the Visual Studio, corresponds to char å.
    it was just an example, extracted from exactly what I'm seeing.

    I don't think my question clear, so let me clarify it:

    I am storing binary code into the char array from a double in the first place. So I really don't care about character representation.

    I am NOT doing this: 45.6777 and store it as characters then convert it to double.

    What I am doing is this:
    1- I have a double say -.1378
    2- All 8 bytes of this double are stored in the char array, each byte is stored into an index
    3- What I need is to read those 8 bytes from the array back into a double
    Last edited by doia; 06-14-2010 at 09:06 AM.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Are you certain you can allocate that much memory on the stack?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    Quote Originally Posted by claudiu View Post
    Are you certain you can allocate that much memory on the stack?
    hasn't given me any issues so far.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Probably union ?
    Code:
    union foo {
        char x[8];
        double real;
    };

  7. #7
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    Quote Originally Posted by Bayint Naung View Post
    Probably union ?
    Code:
    union foo {
        char x[8];
        double real;
    };
    Can you please be more specific?
    The values are stored in a char array. Are you suggesting I transfer the 8 bytes from the char array into the union and then just call the value for double? Isn't there a chance my double will be corrupted?

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    How about
    Code:
    union foo {
        unsigned char x[65536];
        unsigned long long y[8192];
        double z[8192];
    };
    You only want to convert 8 bytes, right? People are worried about an array of size 65536 bytes.
    I'm not certain you really want the floating point representation.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by doia View Post
    What I am doing is this:
    1- I have a double say -.1378
    2- All 8 bytes of this double are stored in the char array, each byte is stored into an index
    3- What I need is to read those 8 bytes from the array back into a double
    as you move bytes of double to char array - move them back in the same way
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    Thanks nonoob and Bayint.
    Quote Originally Posted by nonoob View Post
    You only want to convert 8 bytes, right? People are worried about an array of size 65536 bytes.
    I'm not certain you really want the floating point representation.
    Not really, I need to convert a lot more than just 8 bytes, I am going to have a large amount of values stored into this array. The values stored are going to be in different formats too (double, uint, int, float, etc.), I have a map for this array telling me where each value starts and ends and what data type it should be converted to. I just wanted to get the general idea how to do it.

    as you move bytes of double to char array - move them back in the same way
    The array values comes from a buffer that is made available to my program from an independent process, so I don't know how they were stored there. I wouldn't ask the question if I already had the answer.
    Last edited by doia; 06-14-2010 at 04:01 PM.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You already have the answer. Use a union.
    Code:
    union foo
    {
        char c[sizeof(double)];
        double d;
    } bar;
    
    char buf[ BUFSIZ ];
    char *p = buf;
    ...
    for( this; that; theotherthing )
    {
        memcpy( bar.c, p, sizeof( double ) );
        p += sizeof( double );
        printf( "Let's pretend a double can be represented correctly! %f\n", bar.d );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The easiest is probably to use memcpy(), or make a function that takes a void pointer as argument, then just assign the void pointer to a double pointer, dereference and return. Or do a cast directly, more messy but it works as well.

  14. #14
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    okay I used the union from quzah's code and it works!
    Thanks to everyone

    Isa

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Travel Expenses-Weird Result
    By taj777 in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2010, 02:23 PM
  2. Replies: 4
    Last Post: 10-31-2009, 07:18 PM
  3. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  4. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM