Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to add a double to an unsigned char array?

Reply
Thread Tools

How to add a double to an unsigned char array?

 
 
Angus
Guest
Posts: n/a
 
      09-20-2007
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?

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      09-20-2007
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 = ???????

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-20-2007
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


 
Reply With Quote
 
chikkubhai
Guest
Posts: n/a
 
      09-20-2007
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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-20-2007
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


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-20-2007
"Angus" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      09-20-2007

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


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-21-2007
On Sep 20, 7:49 pm, "Jim Langston" <(E-Mail Removed)> wrote:
> "Angus" <(E-Mail Removed)> wrote in message


> news:(E-Mail Removed) 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:(E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Padding bits and char, unsigned char, signed char Ioannis Vranos C++ 11 03-28-2008 10:47 PM
Linking error LNK2001 - "__declspec(dllimport) private: void __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Tidy(unsigned short)" (__imp_?_Tidy@?$basic_string@DU?$char_ sharmadeep1980@gmail.com C++ 1 07-07-2006 07:27 AM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM



Advertisments