473,761 Members | 8,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2223
On 23 Dec 2004 13:32:54 -0800, "nickisme" <ni********@yah oo.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.l earn.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*****@btinte rnet.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.l earn.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.l earn.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
3222
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: 15.20 decimal will be 1111 as binary...
0
1913
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 at: http://dcs.nac.uci.edu/~strombrg/converting-binary.html So far, the page includes a variety of programs written in C or python to do:
0
338
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 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...
2
7678
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 int type). I want to store the number as binary stream, and I don't know how to convert large decimal string (char*) to binary stream. Converting using MOD would last for hours with really big numbers, and in fact I don't know any other ways to...
15
2559
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 holding the equivalent of that character in the UTF8 format). How can I decode this UTF8-encoded string? In Delphi I could simple say:
4
2398
by: Beginner | last post by:
How do I convert JPEG images to binary files in ASP.NET? Please advice. Thanks.
28
3594
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 business basic. I need to re-write it, bring it kicking and screaming to run on Windows XP. This is for a video rental place, tracks movie and game rentals, customers, employee transactions, reservations, does reports,..... and on. I know some of...
1
1714
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: System::String^ s = gcnew System::String(basic_s.c_str()); //here basic_s is a basic string in c++ But I found s contains less charactors than the original basic string, some charactors are missing.
0
3349
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 engine. There are standard algorithms for converting between representations. See above. Good programmer calculators have these built in.
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.