Converting negative integer to octal/hexadecimal

Discussion in 'C Programming' started by jaks.maths@gmail.com, Jun 6, 2006.

  1. Guest

    How to convert negative integer to hexadecimal or octal number?

    Ex: -568

    What is the equivalent hexadecimal and octal number??
     
    , Jun 6, 2006
    #1
    1. Advertising

  2. bert Guest

    wrote:
    > How to convert negative integer to hexadecimal or octal number?
    >
    > Ex: -568
    >
    > What is the equivalent hexadecimal and octal number??


    +568 = hex ...0000238 = 2 * 16^2 + 3 * 16 + 8
    -568 = hex ...FFFFDC8, verify the sum is zero.

    +568 = oct ...0001070 = 8^3 + 7 * 8
    -568 = oct ...7776710, verify the sum is zero.
    --
     
    bert, Jun 6, 2006
    #2
    1. Advertising

  3. Vladimir Oka Guest

    wrote:
    > How to convert negative integer to hexadecimal or octal number?
    >
    > Ex: -568
    >
    > What is the equivalent hexadecimal and octal number??


    And how is this a C question?

    Even if it were, you were not precise enough. Do you mean 1's or 2's
    complement, sign-plus-absolute value? How many bits wide your numbers
    are?

    >From where I sit, the following program:


    #include <stdio.h>

    int main( void )
    {
    int i = -568;

    printf("i = %x\n", i);

    return 0;
    }

    produces the following:

    i = fffffdc8

    [FYI: that's 32 bit 2's complement. YMMV.]
     
    Vladimir Oka, Jun 6, 2006
    #3
  4. osmium Guest

    <> wrote:

    > How to convert negative integer to hexadecimal or octal number?
    >
    > Ex: -568
    >
    > What is the equivalent hexadecimal and octal number??


    Convert is not quite the right word. As used in the industry, octal and hex
    are *representations* of numbers. Choose your number and a coding
    technique, Write it (perhaps on paper) in binary and then octal and hex are
    shortcut ways of communicating, to people mostly, the binary number you are
    speaking of. There are at least three ways of representing negative
    numbers: one complement, twos complement and sign and magnitude (using a
    "sign box"). They would all be different for your sample number.

    If that bothers you, think of the difference between a thing and a picture
    of that thing.
     
    osmium, Jun 6, 2006
    #4
  5. Eric Sosman Guest

    wrote On 06/06/06 08:59,:
    > How to convert negative integer to hexadecimal or octal number?
    >
    > Ex: -568
    >
    > What is the equivalent hexadecimal and octal number??


    Hexadecimal: -0x238
    Octal: -01070

    (These are not flippant answers. Pay no attention to
    offered "answers" like 0xFFFFFDC8 or 037777776710, because
    they merely perpetuate the confusion between representation
    and value.)

    --
     
    Eric Sosman, Jun 6, 2006
    #5
  6. Vladimir Oka Guest

    Eric Sosman wrote:
    > wrote On 06/06/06 08:59,:
    > > How to convert negative integer to hexadecimal or octal number?
    > >
    > > Ex: -568
    > >
    > > What is the equivalent hexadecimal and octal number??

    >
    > Hexadecimal: -0x238
    > Octal: -01070
    >
    > (These are not flippant answers. Pay no attention to
    > offered "answers" like 0xFFFFFDC8 or 037777776710, because
    > they merely perpetuate the confusion between representation
    > and value.)


    I guess the obviousness of the above correct answer leads to reading
    into the question something that (possibly) wasn't there in the first
    place. (Have the standards of teaching Maths declined that much?) To me
    at least, the intent of the original question is not 100% clear.
     
    Vladimir Oka, Jun 6, 2006
    #6
  7. Eric Sosman Guest

    Vladimir Oka wrote On 06/06/06 10:36,:
    > Eric Sosman wrote:
    >
    >> wrote On 06/06/06 08:59,:
    >>
    >>>How to convert negative integer to hexadecimal or octal number?
    >>>
    >>>Ex: -568
    >>>
    >>>What is the equivalent hexadecimal and octal number??

    >>
    >> Hexadecimal: -0x238
    >> Octal: -01070
    >>
    >> (These are not flippant answers. Pay no attention to
    >>offered "answers" like 0xFFFFFDC8 or 037777776710, because
    >>they merely perpetuate the confusion between representation
    >>and value.)

    >
    >
    > I guess the obviousness of the above correct answer leads to reading
    > into the question something that (possibly) wasn't there in the first
    > place. (Have the standards of teaching Maths declined that much?) To me
    > at least, the intent of the original question is not 100% clear.


    I think the question is clear (of course, I may be
    mistaken!) but misguided. C programmers seem inclined
    to worry about representations when they ought to be
    thinking about values; that leads to "mysteries" like

    short s = 0xFFFF;
    assert (s == 0xFFFF); /* why does it abort? */

    There are, of course, times when the programmer must
    think about representations. When you're converting
    between an internal form and an externally-imposed form,
    representations often (not always!) come into play. When
    you're treating a data object like a bunch of bit fields
    rather than like a number, you're entirely right to be
    thinking about representations. But when you're dealing
    with numbers as counts and quantities and indices and the
    like, think about their values and properties and not about
    their implementations. Much grief is thereby avoided.

    --
     
    Eric Sosman, Jun 6, 2006
    #7
  8. wrote:
    > How to convert negative integer to hexadecimal or octal number?


    You don't convert values, but external representations.

    > Ex: -568
    > What is the equivalent hexadecimal and octal number??



    #include <stdio.h>

    void shownum(int x)
    {
    unsigned v, s = 0;
    s = (x < 0);
    v = (x < 0) ? -x : x;
    printf("%d (decimal) is %s%#x (hex) and %s%#o (octal).\n",
    x, s ? "-" : "", v, s ? "-" : "", v);
    }

    int main(void)
    {
    shownum(-568);
    shownum(568);
    return 0;
    }


    -568 (decimal) is -0x238 (hex) and -01070 (octal).
    568 (decimal) is 0x238 (hex) and 01070 (octal).
     
    Martin Ambuhl, Jun 6, 2006
    #8
  9. CBFalconer Guest

    bert wrote:
    > wrote:
    >
    >> How to convert negative integer to hexadecimal or octal number?
    >>
    >> Ex: -568
    >>
    >> What is the equivalent hexadecimal and octal number??

    >
    > +568 = hex ...0000238 = 2 * 16^2 + 3 * 16 + 8
    > -568 = hex ...FFFFDC8, verify the sum is zero.
    >
    > +568 = oct ...0001070 = 8^3 + 7 * 8
    > -568 = oct ...7776710, verify the sum is zero.


    That assumes 2's complement arithmetic. While common, that is not
    guaranteed. 1's complement and sign magnitude are also possible,
    when the results will be different.

    --
    "Our enemies are innovative and resourceful, and so are we.
    They never stop thinking about new ways to harm our country
    and our people, and neither do we." -- G. W. Bush.
    "The people can always be brought to the bidding of the
    leaders. All you have to do is tell them they are being
    attacked and denounce the pacifists for lack of patriotism
    and exposing the country to danger. It works the same way
    in any country." --Hermann Goering.
     
    CBFalconer, Jun 6, 2006
    #9
  10. Old Wolf Guest

    Vladimir Oka wrote:
    >From where I sit, the following program:
    >
    > #include <stdio.h>
    >
    > int main( void )
    > {
    > int i = -568;
    > printf("i = %x\n", i);
    > return 0;
    > }
    >
    > produces the following:
    >
    > i = fffffdc8
    >
    > [FYI: that's 32 bit 2's complement. YMMV.]


    YMMV indeed because the program causes undefined behaviour:
    the parameter corresponding to %x must be an unsigned int
    (or, arguably, an int with a non-negative value).

    You could replace the line with
    printf( "i = %x\n", (unsigned)i );

    and then it is clear that i is not fffffdc8; it is a different
    number that i has been converted to.
     
    Old Wolf, Jun 7, 2006
    #10
  11. Vladimir Oka Guest

    Old Wolf wrote:
    > Vladimir Oka wrote:
    > >From where I sit, the following program:
    > >
    > > #include <stdio.h>
    > >
    > > int main( void )
    > > {
    > > int i = -568;
    > > printf("i = %x\n", i);
    > > return 0;
    > > }
    > >
    > > produces the following:
    > >
    > > i = fffffdc8
    > >
    > > [FYI: that's 32 bit 2's complement. YMMV.]

    >
    > YMMV indeed because the program causes undefined behaviour:
    > the parameter corresponding to %x must be an unsigned int
    > (or, arguably, an int with a non-negative value).
    >
    > You could replace the line with
    > printf( "i = %x\n", (unsigned)i );
    >
    > and then it is clear that i is not fffffdc8; it is a different
    > number that i has been converted to.


    You are right, of course. I was careless.

    (I do not use printf/scanf family in day to day work.)
     
    Vladimir Oka, Jun 7, 2006
    #11
  12. Guest

    My question is to get cleared about the following.

    Can we have negative octal numbers and hex decimal numbers, Please
    confirm?

    Vladimir Oka wrote:
    > Eric Sosman wrote:
    > > wrote On 06/06/06 08:59,:
    > > > How to convert negative integer to hexadecimal or octal number?
    > > >
    > > > Ex: -568
    > > >
    > > > What is the equivalent hexadecimal and octal number??

    > >
    > > Hexadecimal: -0x238
    > > Octal: -01070
    > >
    > > (These are not flippant answers. Pay no attention to
    > > offered "answers" like 0xFFFFFDC8 or 037777776710, because
    > > they merely perpetuate the confusion between representation
    > > and value.)

    >
    > I guess the obviousness of the above correct answer leads to reading
    > into the question something that (possibly) wasn't there in the first
    > place. (Have the standards of teaching Maths declined that much?) To me
    > at least, the intent of the original question is not 100% clear.
     
    , Jun 23, 2006
    #12
  13. said:

    > My question is to get cleared about the following.
    >
    > Can we have negative octal numbers and hex decimal numbers, Please
    > confirm?


    There is no such thing as an octal number, a hex number, a decimal number,
    or a hex decimal number. There are just numbers. Numbers can be negative.
    Octal, decimal, and hexadecimal are not number systems. They are number
    *representation* systems.

    Whether a C integer type can store a negative integer depends on whether it
    is a signed type. Thus:

    unsigned int i = -0xF; /* i does not store a negative value,
    despite the - sign */
    int j = -0xF; /* j stores a negative value. */

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jun 23, 2006
    #13
  14. Guest

    wrote:

    > My question is to get cleared about the following.
    >
    > Can we have negative octal numbers and hex decimal numbers, Please
    > confirm?


    Can we have them in what context ? In the context or programming ?
    In the context of a mathematics book ? In the context of a money-in
    money-out list ?

    As others have said things like octal or hexadecimal or decimal have
    to do with how to represent a number. As with any other form of
    representation they ultimately depend on established convention.

    Anyway to try and give you some sort of an answer , in every context
    I can think of , adding a minus sign in front of a hexadecimal or octal
    representation of a number will represent a negative number assuming
    that a decimal representation of the same number would be legal in that
    same context.

    For example if it is legal in some context to write -255 then it will
    probably
    also be legal to write -FF or -377 perhaps with some additional
    notation
    to signify that what follows the minus sign is a hex or octal number.

    This reply has come out more convoluted than what I was hoping
    but the question isn't very clear either.

    Spiros Bousbouras
     
    , Jun 23, 2006
    #14
  15. Chris Dollin Guest

    wrote:

    > My question is to get cleared about the following.
    >
    > Can we have negative octal numbers and hex decimal numbers, Please
    > confirm?


    There are no "octal numbers" or "hexadecimal numbers". There are
    numbers /written in/ octal and hexadecimal, just as there are
    numbers /written in/ decimal.

    A lot of the time - such as in casual conversational use - the distinction
    between decimal /numerals/ and the /numbers/ they represent doesn't
    much matter; but programming isn't one of those times.

    You can write some decimal numeral, eg 1066. This represents a positive
    number. You can write some hexadecimal number, eg 0xffff. This also
    represents some positive number. You /can't/ write a decimal numeral
    that represents a negative number, and you /can't/ represent a
    hexadecimal number that represents a negative number either.

    What you /can/ write is a numeral that expresses a number which cannot
    be expressed in the number of bits allocated for its representation
    by its context. For example, when you write 12345678910111213141516,
    its value typically cannot be represented as a C int; similarly
    0xffffffff cannot be represented as a C int if the implementation
    uses 32-bit ints.

    Various things are permitted to happen, and I don't offhand know
    the details, but one outcome that /may/ happen is that the bits of
    the /binary/ numeral that represents the value are stuffed willy-nilly
    into the representation, and a bit that was supposed to represent a
    big and positive value occupies the slot used for a big and /negative/
    value, so that this twos-complement binary numeral represents a
    negative number.

    So a numeral 0xffffffff may end up being a bit-pattern that represents
    a negative number in your implementation. It's not the hexadecimal
    number that's negative: it's the surviving bitsvalue after its been
    pintpotted into the available space.

    Just as on a 16-bit-int implementation, the numeral 40000 may end
    up as a bitpattern the machine thinks represents a negative number.
    This doesn't mean that (the value represented by the decimal number) 40000
    is negative.

    --
    Chris "seeker" Dollin
    "Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
     
    Chris Dollin, Jun 23, 2006
    #15
  16. Eric Sosman Guest

    wrote:
    > My question is to get cleared about the following.
    >
    > Can we have negative octal numbers and hex decimal numbers, Please
    > confirm?
    >
    > Vladimir Oka wrote:
    >
    >>Eric Sosman wrote:
    >>
    >>> wrote On 06/06/06 08:59,:
    >>>
    >>>>How to convert negative integer to hexadecimal or octal number?
    >>>>
    >>>>Ex: -568
    >>>>
    >>>>What is the equivalent hexadecimal and octal number??
    >>>
    >>> Hexadecimal: -0x238
    >>> Octal: -01070
    >>>
    >>> (These are not flippant answers. Pay no attention to
    >>>offered "answers" like 0xFFFFFDC8 or 037777776710, because
    >>>they merely perpetuate the confusion between representation
    >>>and value.)

    >>
    >>I guess the obviousness of the above correct answer leads to reading
    >>into the question something that (possibly) wasn't there in the first
    >>place. (Have the standards of teaching Maths declined that much?) To me
    >>at least, the intent of the original question is not 100% clear.


    In C source, all integer constants expressed in decimal,
    octal, or hexadecimal are non-negative. There is no way to
    write a negative number in any of these bases.

    You can, however, combine constants with operators to
    form expressions with negative values. One easy way of doing
    this is to use the `-' operator as a prefix, as in `-42' or
    `-052' or `-0x2A'. These are not negative integer constants,
    but expressions each consisting of a positive integer constant
    and a `-' operator that negates its value.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 23, 2006
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hostos
    Replies:
    7
    Views:
    5,237
    La'ie Techie
    Oct 15, 2003
  2. Replies:
    2
    Views:
    600
    Christos TZOTZIOY Georgiou
    Dec 24, 2004
  3. KB
    Replies:
    5
    Views:
    772
    Steven D'Aprano
    Jul 31, 2005
  4. Michael Goerz

    converting to and from octal escaped UTF--8

    Michael Goerz, Dec 3, 2007, in forum: Python
    Replies:
    9
    Views:
    1,902
    MonkeeSage
    Dec 4, 2007
  5. Harris Reynolds

    Converting Bytes to a Negative Integer

    Harris Reynolds, Feb 14, 2007, in forum: Ruby
    Replies:
    9
    Views:
    157
    James Edward Gray II
    Feb 16, 2007
Loading...

Share This Page