By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,391 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,391 IT Pros & Developers. It's quick & easy.

Basic question on converting to binary

P: n/a
Hi - sorry for the possibly stupid question, but I'm still a wee
starter on c++... Just wondering if there's a quick way to convert data
into binary strings...

To explain, I'm trying to convert numbers into 16 bit binary form, so
that I can eventually alter the LSB for dithering purposes. Obviously I
can create my own function that will do this, but I was wondering if
there was some quick process that I could call up from a library that
was processor light, and ohhh you know what I'm on about. Maybe. I'm
too tired to carry on.
Anyway, if that makes sense, thanks for any help you can give me!

Nick

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 23 Dec 2004 13:32:54 -0800, "nickisme" <ni********@yahoo.co.uk>
wrote in comp.lang.c++:
Hi - sorry for the possibly stupid question, but I'm still a wee
starter on c++... Just wondering if there's a quick way to convert data
into binary strings...
What are binary strings? Where does the data come from and what form
is it in? Test file, binary file? All integer type data in C++ is
binary internally at all times.
To explain, I'm trying to convert numbers into 16 bit binary form, so
that I can eventually alter the LSB for dithering purposes. Obviously I
can create my own function that will do this, but I was wondering if
there was some quick process that I could call up from a library that
was processor light, and ohhh you know what I'm on about. Maybe. I'm
too tired to carry on.
Anyway, if that makes sense, thanks for any help you can give me!

Nick


Do you want 16 bit integer types (short and unsigned short are that
size on most common platforms), or do you want "binary strings", and
if so, explain what "binary strings".

In your subject line, and repeated in your post, you tell us you want
to "convert to binary" and "convert numbers to 16 bit binary", but you
still haven't told us what the input format looks like.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #2

P: n/a
> All integer type data in C++ is
binary internally at all times.


Well, this is a question we already talked about some time ago... I would
prefer using unsigned types (unsigned char, unsigned short, unsigned int,
unsigned long) over signed ones, because it seems that the C++ Standard
implies that the signed types behave equally to corresponding unsigned ones
just for nonnegative values (while if one or both operands are negative we
have undefined behavior). Am I right ?

Rade
Jul 22 '05 #3

P: n/a
On Thu, 23 Dec 2004 23:32:50 +0000 (UTC), "Rade"
<no*****@btinternet.com> wrote in comp.lang.c++:
All integer type data in C++ is
binary internally at all times.


Well, this is a question we already talked about some time ago... I would
prefer using unsigned types (unsigned char, unsigned short, unsigned int,
unsigned long) over signed ones, because it seems that the C++ Standard
implies that the signed types behave equally to corresponding unsigned ones
just for nonnegative values (while if one or both operands are negative we
have undefined behavior). Am I right ?


I don't understand what you are talking about. Many operations are
well defined for signed integer types as long as overflow and
underflow are avoided. On the other hand, some bit-wise and shift
operands are questionable on the signed types.

Perhaps you are confusing behavior and representation. Both the C and
C++ standards require that when an object of a signed integer type has
a positive value, it must have the identical bit-wise representation
as an object of the corresponding unsigned integer type containing the
same value. But that does not imply that the behavior is the same
under all conditions.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #4

P: n/a
> I don't understand what you are talking about. Many operations are
well defined for signed integer types as long as overflow and
underflow are avoided. On the other hand, some bit-wise and shift
operands are questionable on the signed types.

Perhaps you are confusing behavior and representation. Both the C and
C++ standards require that when an object of a signed integer type has
a positive value, it must have the identical bit-wise representation
as an object of the corresponding unsigned integer type containing the
same value. But that does not imply that the behavior is the same
under all conditions.


The OP wanted to perform *bit-wise* operations on his values, so I just
thought that the behavior of these operations will be out of doubt if he
stores his values in objects of unsigned types.

For example, even if short is 16-bit and even if it uses 2's complement to
store negative integers, C++ Standard seems not to prohibit some of the
binary representations to be illegal (e.g. 0x8000 - perhaps the machine uses
a range -32767 to 32767 for short integers, avoiding -32768). So, for
example, (-32767) ^ 1 is not a valid integer any more (and this is important
to the OP, since he wants to perform manipulations on the LSB). If I still
don't understand things properly, please correct me.

Rade
Jul 22 '05 #5

P: n/a
nickisme wrote:
Hi - sorry for the possibly stupid question, but I'm still a wee
starter on c++... Just wondering if there's a quick way to convert data
into binary strings...

To explain, I'm trying to convert numbers into 16 bit binary form, so
that I can eventually alter the LSB for dithering purposes. Obviously I
can create my own function that will do this, but I was wondering if
there was some quick process that I could call up from a library that
was processor light, and ohhh you know what I'm on about. Maybe. I'm
too tired to carry on.
Anyway, if that makes sense, thanks for any help you can give me!

Nick


Internally, numbers are stored in binary form. I'm not sure
whether you want to produce an ASCII representation of a binary
number or you want to take an ASCII representation and convert
into internal (binary format).

Or do you want to examine individual bits of a number?

Please read the FAQ below and welcome.txt. If those documents
don't help, then post a detailed description (psuedo code will
do} and what you need.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.