473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Basic question on converting to binary

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
5 2164
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
> 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
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sverre Bakke | last post by:
Hi I am using the base_convert() function to convert numbers between binary, hexadecimal, decimal, etc... It works great, but I have problems converting numbers with .'s Like this number: ...
0
by: Dan Stromberg | last post by:
I've written up a page about how to convert native binary data to another platform's native binary data, as I did some fortran data conversions for a client. The programs and documentation are...
0
by: nickisme | last post by:
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...
2
by: Mariusz Sakowski | last post by:
I'm writing class which will be able to store large numbers (my ambition is to make it able to operand on thousands of bits) and perform various operations on it (similiar to those available with...
15
by: C# Learner | last post by:
Hi, I have a string (System.String) which holds some data. This data is encoding in UTF8 (i.e. anywhere in the string where there should be a single 'é' character, there will be two characters...
4
by: Beginner | last post by:
How do I convert JPEG images to binary files in ASP.NET? Please advice. Thanks.
28
by: Randy Reimers | last post by:
(Hope I'm posting this correctly, otherwise - sorry!, don't know what else to do) I wrote a set of programs "many" years ago, running in a type of basic, called "Thoroughbred Basic", a type of...
1
by: Vivienne | last post by:
Hi, I am using a c# library in my c++ project. so I have to convert a basic string to System::string to make use of a function that takes System::String as argument. I did it like this: ...
0
by: Terry Reedy | last post by:
A. Joseph wrote: These are number representation systems that can be applied to or used with integral, rational (numberator,denominator), and 'point' numbers. Try Wikipedia or any search...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.