473,387 Members | 1,476 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.

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

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
Jul 19 '05 #1
4 22138
WW
arvind wrote:
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.

--
WW aka Attila
Jul 19 '05 #2
arvind wrote:
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::oct << c << std::endl;

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3
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.

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:DC***************@newsread4.news.pas.earthlin k.net...
arvind wrote:
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::oct << c << std::endl;

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4
Mohamed Ghouse wrote:

Please don't top-post.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.4
Borland C/C++ has in its library a function called itoa.


This group is for discussion of Standard C++, not Borland C++.

http://www.slack.net/~shiva/welcome.txt
http://www.slack.net/~shiva/offtopic.txt

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5

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

Similar topics

5
by: Blaktyger | last post by:
How can this be done? Images are stored in a LONGBLOB field. When I try to display them, it prints out the binary data as it is... Thank you
2
by: dmiller23462 | last post by:
Hey guys, I'm back again....I've got the results displaying that I wanted but instead of having a "next" or "previous" link displayed I am getting the error msg below (I actually get the data that...
10
by: J. Campbell | last post by:
OK...I'm in the process of learning C++. In my old (non-portable) programming days, I made use of binary files a lot...not worrying about endian issues. I'm starting to understand why C++ makes...
6
by: Andrew | last post by:
Hi I have a question is there a function in C++ to convert an integer into a Binary number Thanks in Advance Cheers
1
by: DiskMan | last post by:
System: Redhat 7.2 Kernel-2.6.11.8 GCC-3.4.3 CCC-6.5.9 Binutils-2.15 Make-3.80 GTK/GLIB-2.6.7 For some reason my Linux box is suddenly having issues trying to read ;
8
by: Lucas | last post by:
I need print a file in binary mode . f = f.open('python.jpg','rb') bytes = f.read() f.close() print(bytes) I can't get any binary code.
0
by: aled | last post by:
I'm new to C++ and need help with a binary program, represented in the program as an array of bits (0's and 1's). It allows the user to input a binary word, then do some calculations or...
5
by: wshaer | last post by:
Hi This is the task: and these are my classes: public class Engine{ // Declare the varibles
4
by: Mason | last post by:
I have tried and tried... I'd like to read in a binary file, convert it's 4 byte values into floats, and then save as a .txt file. This works from the command line (import struct); In : f =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
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.