How to add a double to an unsigned char array?

A

Angus

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?
 
G

Gianni Mariani

Angus said:
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 = ???????
 
V

Victor Bazarov

Angus said:
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
 
C

chikkubhai

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
 
V

Victor Bazarov

chikkubhai said:
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
 
J

Jim Langston

Angus said:
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.
 
B

BobR

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.
 
J

James Kanze

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top