how to represent binary code , hex code in c++ and display them on screen using prinf statement

A

arvind

hi,
How do I display a number in binary or octagonal ?

eg.
int a= 195; //1100 0011
int b = 87; //0101 0111
int c = a&b;//0100 0011 => bitwise and

printf("%0x", c); // Here I get the hexagonal output
//I want to get the binary output of char c
// I also want the Octagonal Outpur of char c

Please let me how to the output in the binary and octagonal form.

Thanks in advance,
-arvind
 
W

WW

arvind said:
hi,
How do I display a number in binary or octagonal ?

eg.
int a= 195; //1100 0011
int b = 87; //0101 0111
int c = a&b;//0100 0011 => bitwise and

printf("%0x", c); // Here I get the hexagonal output
//I want to get the binary output of char c
// I also want the Octagonal Outpur of char c

Please let me how to the output in the binary and octagonal form.

For binaryu you need to write your own functions to convert numbers into a
sequence of 1 and 0 characters. If you want something octagonal, you will
need graphics:

http://www.fastgeometry.com/images/octagon.gif
http://www.fastgeometry.com/Encyclopedia/Polygons.htm

However if you want *octal*, you will need to use o instead of the x.
 
K

Kevin Goodsell

arvind said:
hi,
How do I display a number in binary or octagonal ?

It's called "octal". "Octagon" is a geometric figure with 8 sides of
equal length.
eg.
int a= 195; //1100 0011
int b = 87; //0101 0111
int c = a&b;//0100 0011 => bitwise and

printf("%0x", c); // Here I get the hexagonal output
//I want to get the binary output of char c
// I also want the Octagonal Outpur of char c

No, here you get undefined behavior. The "%x" format specifier expects
an unsigned int. If you pass it the wrong type (as you have here), the
behavior is undefined. This is one reason to never use printf or any
other function or language feature which defeats type-checking if you
can possibly avoid it.

And it's called "Hexadecimal". "Hexagon" is a geometric figure with 6
sides of equal length.
Please let me how to the output in the binary and octagonal form.

There is no standard way to output binary, you have to work it out for
yourself.

Octal can be done using the "%o" printf format specifier (which also
expects an unsigned int, so you'll need to convert or use the right type
to start with if you want to use it). Better yet, look up the 'oct'
stream modifier:

std::cout << std::eek:ct << c << std::endl;

-Kevin
 
M

Mohamed Ghouse

Borland C/C++ has in its library a function called itoa. I don't remember
the exact signature of the function.
But one of the parameter had the parameter called __base__. With this a
conversion from decimal to any
base was possible.

-Ghouse

One of the params was to the base of a number. But there is no such standard
function defined.
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top