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

From: Mohamed Ghouse (ghouse_at_emanate.info)
Date: 10/04/03


Date: Sat, 4 Oct 2003 21:57:04 +0200

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" <usenet1.spamfree.fusion@neverbox.com> wrote in message
news:DCEfb.1549$Qy2.77@newsread4.news.pas.earthlink.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.
>



Relevant Pages

  • Re: Please help optimize (and standarize) this code...
    ... > where there's no itoa(). ... Do you suggest sprintf, or is there a standard, specific ... > Why don't you use sprintfto make up the format string? ... if you give sizeof the name of a real instance of the NameExt ...
    (comp.lang.c)
  • Re: The usage of %p in C
    ... The standard requires signed int and unsigned int to have the same ... same representation. ... DS9K is probably the only implementation where the above would fail ...
    (comp.lang.c)
  • Re: Unions in structures
    ... so I wondered if this was part of the standard. ... struct thing { ... unsigned int willingFlag: 1; ... The "dubious" part here is that the compiler has a lot ...
    (comp.lang.c)
  • Re: Portable way to printf() a size_t instance
    ... >> which this International Standard imposes no requirements". ... > You don't know if size_t is a typedef for unsigned int. ... The standard talks about undefined behavior, ...
    (comp.lang.c)
  • Re: BETWEEN
    ... it uses the wrap-around properties of ... can do something about the stack shuffling by using locals: ... New standard: http://www.forth200x.org/forth200x.html ...
    (comp.lang.forth)