formatting binary output

Z

zgfareed

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

Jack Klein

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
 
J

Jerry Coffin

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

Barry

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

Zara

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<8> number(145);
std::string representation(number.to_string());
std::cout
<<representation.substr(0,4)<<' '
<<representation.substr(4)<<std::endl;
}


Best regards,

Zara
 
B

Barry

Zara said:
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<8> number(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
 
J

Jim Langston

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

Zara

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
 
J

James Kanze

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

Barry

Zara said:
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"
 

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
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top