473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

Jun 6 '06 #2

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, 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********@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.
Jun 6 '06 #4


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

Jun 6 '06 #5

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.

Jun 6 '06 #6


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

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

Jun 6 '06 #7
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).
Jun 6 '06 #8
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.
Jun 6 '06 #9
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.

Jun 7 '06 #10

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

Jun 7 '06 #11
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.


Jun 23 '06 #12
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)
Jun 23 '06 #13

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 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 '06 #14
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 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/

Jun 23 '06 #15
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 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
es*****@acm-dot-org.invalid
Jun 23 '06 #16

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

Similar topics

2
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: ...
20
by: Sean McIlroy | last post by:
I recently found out that unicode("\347", "iso-8859-1") is the lowercase c-with-cedilla, so I set out to round up the unicode numbers of the extra characters you need for French, and I found them...
5
by: Bunyip Bluegum | last post by:
I have a text field in a form which I need to check to see that only a 4-digit integer has been entered. The field has MAXLENGTH=4 and I'm using this to check for length: function...
33
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:...
13
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...
5
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...
7
by: Gary Brown | last post by:
Hi, I have a whole bunch of integer constants that are best given in octal (PDP-1 opcodes). It makes a huge difference in readability and authenticity if these can be entered in octal. I...
4
by: =?ISO-8859-1?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 compile-time constants: #define BIN8(n)\ (((0x##n##ul&1<<...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.