Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to represent binary code , hex code in c++ and display them on screen using prinf statement

Reply
Thread Tools

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

 
 
arvind
Guest
Posts: n/a
 
      10-04-2003
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
 
Reply With Quote
 
 
 
 
WW
Guest
Posts: n/a
 
      10-04-2003
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


 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      10-04-2003
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:ct << c << std::endl;

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

 
Reply With Quote
 
Mohamed Ghouse
Guest
Posts: n/a
 
      10-04-2003
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" <(E-Mail Removed)> wrote in message
newsCEfb.1549$(E-Mail Removed) 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:ct << c << std::endl;
>
> -Kevin
> --
> My email address is valid, but changes periodically.
> To contact me please use the address from a recent posting.
>



 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      10-04-2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
search for hex characters in a binary file and remove them venkateshwar D Perl Misc 11 08-20-2009 08:37 PM
can we use such function to display input without using "Prinf" ? varojee@gmail.com C Programming 6 12-01-2006 07:57 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments