Velocity Reviews > Converting negative integer to octal/hexadecimal

# Converting negative integer to octal/hexadecimal

jaks.maths@gmail.com
Guest
Posts: n/a

 06-06-2006
How to convert negative integer to hexadecimal or octal number?

Ex: -568

What is the equivalent hexadecimal and octal number??

bert
Guest
Posts: n/a

 06-06-2006
(E-Mail Removed) 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.
--

Guest
Posts: n/a

 06-06-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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.]

osmium
Guest
Posts: n/a

 06-06-2006
<(E-Mail Removed)> 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.

Eric Sosman
Guest
Posts: n/a

 06-06-2006

(E-Mail Removed) 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??

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.)

--
(E-Mail Removed)

Guest
Posts: n/a

 06-06-2006

Eric Sosman wrote:
> (E-Mail Removed) 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??

>
> 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.)

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.

Eric Sosman
Guest
Posts: n/a

 06-06-2006

Vladimir Oka wrote On 06/06/06 10:36,:
> Eric Sosman wrote:
>
>>(E-Mail Removed) 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??

>>
>> 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.)

>
>
> 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

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
their implementations. Much grief is thereby avoided.

--
(E-Mail Removed)

Martin Ambuhl
Guest
Posts: n/a

 06-06-2006
(E-Mail Removed) 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(-56;
shownum(56;
return 0;
}

-568 (decimal) is -0x238 (hex) and -01070 (octal).
568 (decimal) is 0x238 (hex) and 01070 (octal).

CBFalconer
Guest
Posts: n/a

 06-06-2006
bert wrote:
> (E-Mail Removed) 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.

Old Wolf
Guest
Posts: n/a

 06-07-2006
>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.