How to convert negative integer to hexadecimal or octal number?
Ex: 568
What is the equivalent hexadecimal and octal number?? 15 35337
jaks.ma...@gmail.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.
 ja********@gmail.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, signplusabsolute 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.]
<ja********@gmail.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 *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. ja********@gmail.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??
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.)
 Er*********@sun.com
Eric Sosman wrote: ja********@gmail.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??
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 wrote On 06/06/06 10:36,: Eric Sosman wrote:
ja********@gmail.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??
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 externallyimposed 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.
 Er*********@sun.com ja********@gmail.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).
bert wrote: jaks.ma...@gmail.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.
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 nonnegative 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 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 nonnegative 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.)
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: ja********@gmail.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??
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. ja********@gmail.com 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)
jaks.ma...@gmail.com 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 moneyin
moneyout 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 ja********@gmail.com 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 32bit 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 willynilly
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 twoscomplement binary numeral represents a
negative number.
So a numeral 0xffffffff may end up being a bitpattern 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 16bitint 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/ ja********@gmail.com 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:
ja********@gmail.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??
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 nonnegative. 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 es*****@acmdotorg.invalid This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sverre Bakke 
last post by:
Hi
I am using the base_convert() function to convert numbers between binary,
hexadecimal, decimal, etc... It works great, but I have problems converting
numbers with .'s
Like this number:
...

by: Sean McIlroy 
last post by:
I recently found out that unicode("\347", "iso88591") is the
lowercase cwithcedilla, so I set out to round up the unicode numbers
of the extra characters you need for French, and I found them...

by: Bunyip Bluegum 
last post by:
I have a text field in a form which I need to check to see that only a
4digit integer has been entered. The field has MAXLENGTH=4 and I'm
using this to check for length:
function...

by: gk245 
last post by:
I mean, anything that follows a 0 is automatically turned into a octal
number. I want to have a integer variable that will hold numbers as
integers even if they begin with a zero.
for example:...

by: Freaker85 
last post by:
Hello,
I am new at programming in C and I am searching a manner to parse a
string into an integer.
I know how to do it in Java, but that doesn't work in C ;o)
I searched the internet but I...

by: Jeff Dillon 
last post by:
How might I convert a string like 10.A (in hex) to it's decimal equivalent?
Basically I have an input string like ((1F.A + 3A.D)  1F.E) and need to
calculate the result. Using Reflection and...

by: Gary Brown 
last post by:
Hi,
I have a whole bunch of integer constants that are best given in octal
(PDP1 opcodes). It makes a huge difference in readability and authenticity
if these
can be entered in octal. I...

by: =?ISO88591?Q?Tom=E1s_=D3_h=C9ilidhe?= 
last post by:
Here's a macro that Mathew Hendry posted back in the year 2000 for
achieving binary integer literals that evaluate to compiletime
constants:
#define BIN8(n)\
(((0x##n##ul&1<<...

by: Terry Reedy 
last post by:
A. Joseph wrote:
These are number representation systems that can be applied to or used
with integral, rational (numberator,denominator), and 'point' numbers.
Try Wikipedia or any search...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
For other local times, please check World Time Buddy
In...

by: jianzs 
last post by:
Introduction
Cloudnative applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...

by: mar23 
last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...

by: abbasky 
last post by:
### Vandf component communication method one: data sharing
Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...

by: jimatqsi 
last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...

by: isladogs 
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...

by: fareedcanada 
last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...

by: stefan129 
last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multidomain SSL certificates? Any recommendations on reliable providers or specific...

by: MeoLessi9 
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
 