473,836 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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********@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??


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********@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??


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

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

Similar topics

2
3225
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: 15.20 decimal will be 1111 as binary...
20
2219
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 all just fine EXCEPT for the o-e ligature (oeuvre, etc). I examined the unicode characters from 0 to 900 without finding it; then I looked at www.unicode.org but the numbers I got there (0152 and 0153) didn't work. Can anybody put a help on me wrt...
5
26783
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 checkpostcode(form) { var min=4; if (form.postcode.value.length < min) { alert("Please enter a 4-digit postcode"); return false; }
33
2398
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: int 08453 should print 08453 if i tried to use printf() on it, and not some octal number.
13
3231
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 didn't found it yet. help please thank you Freaker85
5
5589
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 dynamic compilation, I was able to create a VB.NET equivalent of the older Eval function, but it only works with decimal numbers. Any thoughts? I know the logic to convert, but I was hoping for some built-in .NET methods that I could leverage jeff
7
11381
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 haven't found a way to do this. Is there one? Thanks, Gary
4
2574
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)>0)|((0x##n##ul&1<< 4)>3)\ |((0x##n##ul&1<< 8)>6)|((0x##n##ul&1<<12)>9)\ |((0x##n##ul&1<<16)>>12)|((0x##n##ul&1<<20)>>15)\ |((0x##n##ul&1<<24)>>18)|((0x##n##ul&1<<28)>>21))
0
3359
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 engine. There are standard algorithms for converting between representations. See above. Good programmer calculators have these built in.
0
10846
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10594
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9376
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6979
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5650
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5828
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4458
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.