How to add a double to an unsigned char array?

Discussion in 'C++' started by Angus, Sep 20, 2007.

  1. Angus

    Angus Guest

    I am writing a class which represents a byte array - ie an array of
    unsigned char. So far I have worked out how to add strings, integers,
    WORD's, etc. But how would I add a double?

    Would I take the whole number part and the remainder and add the
    individual numbers? Some other way?
    Angus, Sep 20, 2007
    #1
    1. Advertising

  2. Angus wrote:
    > I am writing a class which represents a byte array - ie an array of
    > unsigned char. So far I have worked out how to add strings, integers,
    > WORD's, etc. But how would I add a double?
    >
    > Would I take the whole number part and the remainder and add the
    > individual numbers? Some other way?
    >



    givr a concrete example of what you mean:

    { 0x1, 0x2 } + 2.2 = ???????
    Gianni Mariani, Sep 20, 2007
    #2
    1. Advertising

  3. Angus wrote:
    > I am writing a class which represents a byte array - ie an array of
    > unsigned char. So far I have worked out how to add strings, integers,
    > WORD's, etc. But how would I add a double?


    What does it mean to "add a double"? What if the double is tiny?
    Do you expect all those leading zeros? What if it's very large? Do
    you need all those trailing zeros?

    > Would I take the whole number part and the remainder and add the
    > individual numbers? Some other way?


    Why not convert the double into a string using whatever methods
    available (like sprintf) and then append the string to your string?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 20, 2007
    #3
  4. Angus

    chikkubhai Guest

    Based on the same concept/question, if we add a double and an int the
    result is a double by default.

    what if we wanted to add a char and a double?

    I also see sometimes bit wise manipulations with integers and binary
    digits?

    How are all these possible and where can I learn more about this.

    Regards
    Kris wt a k
    chikkubhai, Sep 20, 2007
    #4
  5. chikkubhai wrote:
    > Based on the same concept/question, if we add a double and an int the
    > result is a double by default.
    >
    > what if we wanted to add a char and a double?


    double wins.

    > I also see sometimes bit wise manipulations with integers and binary
    > digits?


    Are you asking?

    > How are all these possible and where can I learn more about this.


    It's all possible thanks to the magic inside the computer. What
    exactly do you wish to learn? What book on C++ are you reading
    that doesn't explain bit operations?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 20, 2007
    #5
  6. Angus

    Jim Langston Guest

    "Angus" <> wrote in message
    news:...
    >I am writing a class which represents a byte array - ie an array of
    > unsigned char. So far I have worked out how to add strings, integers,
    > WORD's, etc. But how would I add a double?
    >
    > Would I take the whole number part and the remainder and add the
    > individual numbers? Some other way?


    What, exactly, are you trying to accomplish? Do you wish to copy the raw
    bytes of the double to the char array? Something like this?

    double Foo = 123.456;
    unsigned char* Bar = new unsigned char[sizeof(double)];
    memcpy( Bar, reinterpret_cast<char*>( &Foo), sizeof( double ) );

    There are other ways, this is one way of many, there was a thread dscussing
    ways to do just this a while back, maybe 6 months ago.
    Jim Langston, Sep 20, 2007
    #6
  7. Angus

    BobR Guest

    chikkubhai wrote in message...
    > Based on the same concept/question, if we add a double and an int the
    > result is a double by default.
    > what if we wanted to add a char and a double?


    {
    char smnum( 123 );
    double dnum( 3.14 );
    double total = dnum + smnum;
    std::cout<<" double dmun + char smnum = "<<total;
    }
    // double dmun + char smnum = 126.14

    >
    > I also see sometimes bit wise manipulations with integers and binary
    > digits?


    Example?

    >
    > How are all these possible and where can I learn more about this.


    Read your book.

    --
    Bob R
    POVrookie
    BobR, Sep 20, 2007
    #7
  8. Angus

    James Kanze Guest

    On Sep 20, 7:49 pm, "Jim Langston" <> wrote:
    > "Angus" <> wrote in message


    > news:...


    > >I am writing a class which represents a byte array - ie an array of
    > > unsigned char. So far I have worked out how to add strings, integers,
    > > WORD's, etc. But how would I add a double?


    > > Would I take the whole number part and the remainder and add the
    > > individual numbers? Some other way?


    > What, exactly, are you trying to accomplish?


    That is the real question.

    > Do you wish to copy the raw bytes of the double to the char
    > array?


    That's easy (as you show). On the other hand, it's not really
    useful for anything.

    To be useful, you have to represent the double in some defined
    format. In such cases, I would almost always recommend a text
    format, say something like the "%.17g" of C. (This guarantees
    that you can reread exactly the same value you wrote.) His
    mention of "byte array", however, makes me wonder if he isn't
    thinking of something along the lines of what Java does. For
    Java compatibility, he has two solutions:

    -- For maximum portability, he needs to break the floating
    point down into separate sign, exponant and mantissa fields,
    and insert the corresponding bits where they belong. (I
    presented code to do this not too long ago here. With ldexp
    and frexp, it's not that difficult. Surprisingly enough, at
    least on my machine, the performance isn't that bad either.)

    -- If he is willing to restrict himself to machines using IEEE
    format floating point, some sort of type punning can be used
    to treat the floating point as an unsigned integer of the
    same size, and to output that. Formally, just about
    anything involving such type punning is undefined behavior,
    but practically, there will be at least one way which works
    in any given implementation.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 21, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,485
    Victor Bazarov
    Feb 17, 2005
  2. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    579
    Jack Klein
    May 9, 2005
  3. Ioannis Vranos
    Replies:
    11
    Views:
    758
    Ioannis Vranos
    Mar 28, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    787
    James Kanze
    Oct 13, 2008
  5. Gabriel Genellina
    Replies:
    0
    Views:
    514
    Gabriel Genellina
    Aug 13, 2009
Loading...

Share This Page