473,467 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

formatting binary output

My program converts decimals to binary, hex etc. The output of the
binary numbers are to be in the form 0000 0000 for all numbers from 0
to 300. Does anyone have any suggestions as how to get the space
between each 4 bits?
Thanks in advance.

Sep 13 '07 #1
10 2802
On Wed, 12 Sep 2007 18:44:00 -0700, zg******@gmail.com wrote in
comp.lang.c++:
My program converts decimals to binary, hex etc. The output of the
binary numbers are to be in the form 0000 0000 for all numbers from 0
to 300. Does anyone have any suggestions as how to get the space
between each 4 bits?
Thanks in advance.
Your requirement is impossible to meet. The value 300 in binary is
100101100, with spaces it is 1 0010 1100. There is no way to present
the value of 300 in binary as text using fewer than 9 characters.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Sep 13 '07 #2
Pardon me.What about only the values 0 to 255,with 255 being 1111 1111?

Sep 13 '07 #3
In article <11**********************@g4g2000hsf.googlegroups. com>,
zg******@gmail.com says...
Pardon me.What about only the values 0 to 255,with 255 being 1111 1111?
The obvious way would be to create an array (or vector) or strings, one
for each possible value of a nybble (16 possible values). Use the type
four bits as an index, print out the chosen string, print a space, then
print out the string indexed with the bottom four bits.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 13 '07 #4
zg******@gmail.com wrote:
My program converts decimals to binary, hex etc. The output of the
binary numbers are to be in the form 0000 0000 for all numbers from 0
to 300. Does anyone have any suggestions as how to get the space
between each 4 bits?
Thanks in advance.
if you use recursive way, then you have no idea how long is the binary
number, so it's impossible to print the space character.

so you have to borrow extra storage and traverse the storage from end to
begin like Jerry mentioned else thread.

--
Thanks
Barry
Sep 13 '07 #5
On Wed, 12 Sep 2007 18:44:00 -0700, zg******@gmail.com wrote:
>My program converts decimals to binary, hex etc. The output of the
binary numbers are to be in the form 0000 0000 for all numbers from 0
to 300. Does anyone have any suggestions as how to get the space
between each 4 bits?

You may use std::bitset to store the number, and use to_string member
function.

#include <string>
#include <bitset>
#include <iostream>

int main()
{
std::bitset<8number(145);
std::string representation(number.to_string());
std::cout
<<representation.substr(0,4)<<' '
<<representation.substr(4)<<std::endl;
}
Best regards,

Zara
Sep 13 '07 #6
Zara wrote:
On Wed, 12 Sep 2007 18:44:00 -0700, zg******@gmail.com wrote:
>My program converts decimals to binary, hex etc. The output of the
binary numbers are to be in the form 0000 0000 for all numbers from 0
to 300. Does anyone have any suggestions as how to get the space
between each 4 bits?


You may use std::bitset to store the number, and use to_string member
function.

#include <string>
#include <bitset>
#include <iostream>

int main()
{
std::bitset<8number(145);
std::string representation(number.to_string());
std::cout
<<representation.substr(0,4)<<' '
<<representation.substr(4)<<std::endl;
}

using bitset and its to_string()
in this way, you have to ignore all the heading '0's

--
Thanks
Barry
Sep 13 '07 #7
<zg******@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
My program converts decimals to binary, hex etc. The output of the
binary numbers are to be in the form 0000 0000 for all numbers from 0
to 300. Does anyone have any suggestions as how to get the space
between each 4 bits?
Thanks in advance.
Many ways, depending on how you are outputing the binary value. It may be
as simple as putting in a << " " << somewhere
Show us how you are currently outputting/formatting your output.
Sep 13 '07 #8
On Thu, 13 Sep 2007 16:10:48 +0800, Barry <dh*****@gmail.comwrote:
>Zara wrote:
>On Wed, 12 Sep 2007 18:44:00 -0700, zg******@gmail.com wrote:
>>My program converts decimals to binary, hex etc. The output of the
binary numbers are to be in the form 0000 0000 for all numbers from 0
to 300. Does anyone have any suggestions as how to get the space
between each 4 bits?


You may use std::bitset to store the number, and use to_string member
function.

#include <string>
#include <bitset>
#include <iostream>

int main()
{
std::bitset<8number(145);
std::string representation(number.to_string());
std::cout
<<representation.substr(0,4)<<' '
<<representation.substr(4)<<std::endl;
}


using bitset and its to_string()
in this way, you have to ignore all the heading '0's
No, because t to_string generates exactly eisht characters, as
requested by OP

Sep 13 '07 #9
On Sep 13, 3:44 am, zgfar...@gmail.com wrote:
My program converts decimals to binary, hex etc. The output of the
binary numbers are to be in the form 0000 0000 for all numbers from 0
to 300. Does anyone have any suggestions as how to get the space
between each 4 bits?
See my response in the other thread you started ("Conversions
from decimal to binary,hex"). There are several different
solutions.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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
Sep 13 '07 #10
Zara wrote:
On Thu, 13 Sep 2007 16:10:48 +0800, Barry <dh*****@gmail.comwrote:
>Zara wrote:
>>On Wed, 12 Sep 2007 18:44:00 -0700, zg******@gmail.com wrote:

My program converts decimals to binary, hex etc. The output of the
binary numbers are to be in the form 0000 0000 for all numbers from 0
to 300. Does anyone have any suggestions as how to get the space
between each 4 bits?

You may use std::bitset to store the number, and use to_string member
function.

#include <string>
#include <bitset>
#include <iostream>

int main()
{
std::bitset<8number(145);
std::string representation(number.to_string());
std::cout
<<representation.substr(0,4)<<' '
<<representation.substr(4)<<std::endl;
}

using bitset and its to_string()
in this way, you have to ignore all the heading '0's

No, because t to_string generates exactly eisht characters, as
requested by OP
Well,
if "0011 1100" is OK, then OK
I thought for a binary number it would prettier to write "11 1100"

--
Thanks
Barry
Sep 13 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Zigenfous | last post by:
Hello, Here is the code. followed by an explanation for what I am trying to do. In particular, this line: BinaryStream.Write byteArray 'Need to format <%Response.Buffer=0 %> <!--#include...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
1
by: Michael | last post by:
I have a solution for this, but it feels wrong. If anyone could offer a better one, I'm all ears. (Or technically, eyes.) Basically, I have a bunch of classes. For concreteness, one is a...
2
by: bryars | last post by:
I want to write some SQL which results in an automatic conversion of a datetime to a string in a format suitable for the Language of the connection (either by explicitly setting the Language in the...
9
by: john coltrane | last post by:
Is there way to create a formatted string in a similar that is similar to sprintf? The same for printing, printf? C,D,E,F,G,N,X for currency, decimal, exponential, fixed, general, numerical,...
4
by: Ian Semmel | last post by:
If I have an Int64 which is $$$$$cc, what is the best way to format it to a string '$nnnn.cc' ?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.