446,213 Members | 1,117 Online Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

# Converting negative integer to octal/hexadecimal

 P: n/a 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 Replies

 P: n/a 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

 P: n/a 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 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

 P: n/a 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

 P: n/a 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

 P: n/a 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

 P: n/a 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 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

 P: n/a 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

 P: n/a Vladimir Oka wrote:From where I sit, the following program: #include 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

 P: n/a Old Wolf wrote: Vladimir Oka wrote:From where I sit, the following program: #include 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

 P: n/a 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

 P: n/a 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

 P: n/a 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 