Basic question on converting to binary

N

nickisme

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
 
J

Jack Klein

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

Rade

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
 
J

Jack Klein

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

Rade

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
 
T

Thomas Matthews

nickisme said:
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
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top