473,836 Members | 1,595 Online

# Converting negative integer to octal/hexadecimal

How to convert negative integer to hexadecimal or octal number?

Ex: -568

What is the equivalent hexadecimal and octal number??

Jun 6 '06 #1
15 35530
jaks.ma...@gmai l.com 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.
--

Jun 6 '06 #2

ja********@gmai l.com 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.]

Jun 6 '06 #3
<ja********@gma il.com> 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 *representation s* 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.
Jun 6 '06 #4

ja********@gmai l.com 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.)

--
Er*********@sun .com

Jun 6 '06 #5

Eric Sosman wrote:
ja********@gmai l.com 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.

Jun 6 '06 #6

Vladimir Oka wrote On 06/06/06 10:36,:
Eric Sosman wrote:
ja********@gm ail.com 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.

--
Er*********@sun .com

Jun 6 '06 #7
ja********@gmai l.com 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).
Jun 6 '06 #8
bert wrote:
jaks.ma...@gmai l.com 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.
Jun 6 '06 #9
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.

Jun 7 '06 #10

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