469,945 Members | 1,809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

How to add a double to an unsigned char array?

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?

Sep 20 '07 #1
7 7454
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 = ???????

Sep 20 '07 #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?
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
Sep 20 '07 #3
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

Sep 20 '07 #4
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
Sep 20 '07 #5
"Angus" <an*********@gmail.comwrote in message
news:11*********************@19g2000hsx.googlegrou ps.com...
>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.
Sep 20 '07 #6

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
Sep 20 '07 #7
On Sep 20, 7:49 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Angus" <anguscom...@gmail.comwrote in message
news:11*********************@19g2000hsx.googlegrou ps.com...
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:ja*********@gmail.com
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

Sep 21 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by jeko | last post: by
18 posts views Thread by No Such Luck | last post: by
69 posts views Thread by fieldfallow | last post: by
25 posts views Thread by Digital Puer | last post: by
4 posts views Thread by Gernot Frisch | last post: by
5 posts views Thread by anders.weitman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.