# converting decimal to hex

**URL:** <https://boards.straightdope.com/t/converting-decimal-to-hex/35747>\
**Category:** Factual Questions\
**Created:** [October 5, 2000, 7:47am UTC](https://boards.straightdope.com/t/converting-decimal-to-hex/35747 "2000-10-05T07:47:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![brods](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brods](https://boards.straightdope.com/u/brods)\
**Post date:** [October 5, 2000, 7:47am UTC](https://boards.straightdope.com/t/converting-decimal-to-hex/35747/1 "2000-10-05T07:47:47Z")

</div>

How do you convert a negative number to hex.

e.g. -127 converts to 81H

What is the proceedure to work this out without using a calculator?

---

<div class="post-metadata">

**Author:** ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)\
**Post date:** [October 5, 2000, 9:16am UTC](https://boards.straightdope.com/t/converting-decimal-to-hex/35747/2 "2000-10-05T09:16:57Z")

</div>

First, convert the absolute value to a hexadecimal number.  
Second, invert the bits.  
Third, add 1.

127 = 0x7f

~0x7f = 0x80

0x80 + 0x1 = 0x81

---

<div class="post-metadata">

**Author:** ![waterj2](https://avatars.discourse-cdn.com/v4/letter/w/858c86/32.png) [@waterj2](https://boards.straightdope.com/u/waterj2)\
**Post date:** [October 5, 2000, 10:17am UTC](https://boards.straightdope.com/t/converting-decimal-to-hex/35747/3 "2000-10-05T10:17:46Z")

</div>

How do you know that 0x81 isn’t being used to represent 129?

---

<div class="post-metadata">

**Author:** ![ticker](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@ticker](https://boards.straightdope.com/u/ticker)\
**Post date:** [October 5, 2000, 10:28am UTC](https://boards.straightdope.com/t/converting-decimal-to-hex/35747/4 "2000-10-05T10:28:17Z")

</div>

> [@](#):
>
> \*Originally posted by waterj2 \*  
> \*\*How do you know that 0x81 isn’t being used to represent 129? \*\*

Hex represents raw data. Computers can interpret that in several ways. Before you can make sense of it you need to know what representation is being used.

BTW the method described by Punoqllads is known as ‘Twos compliment’ and is used by most computers, certainly all PCs. ‘Ones compliment’ avoids the ‘add one’ step, is easier to convert in your head, but does have the annoying property of allowing both positive and negative zero!

---

<div class="post-metadata">

**Author:** ![brods](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brods](https://boards.straightdope.com/u/brods)\
**Post date:** [October 5, 2000, 10:32am UTC](https://boards.straightdope.com/t/converting-decimal-to-hex/35747/5 "2000-10-05T10:32:32Z")

</div>

Thanks very much,

in case you are wondering i am trying to trace through a small mc6800 program.

an was curious why i was getting different test data this would explain a few things

again  
cheers

---

<div class="post-metadata">

**Author:** ![RM\_Mentock](https://avatars.discourse-cdn.com/v4/letter/r/e274bd/32.png) [@RM\_Mentock](https://boards.straightdope.com/u/RM_Mentock)\
**Post date:** [October 5, 2000, 11:32am UTC](https://boards.straightdope.com/t/converting-decimal-to-hex/35747/6 "2000-10-05T11:32:58Z")

</div>

I’ve thought about it, and I’m stuck. I gotta know. Why are you tracing through a hex program without knowing how to represent hex numbers? Are you supremely overconfident, a lawyer perhaps?

---

<div class="post-metadata">

**Author:** ![brods](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brods](https://boards.straightdope.com/u/brods)\
**Post date:** [October 5, 2000, 2:21pm UTC](https://boards.straightdope.com/t/converting-decimal-to-hex/35747/7 "2000-10-05T14:21:57Z")

</div>

i am studying assembly code at uni, and its been about 6 years since i had to do this without a calculator and finding any resources about 1’s and 2’s compliment to hex with the negative flags etc. has been difficult to find, but i’m ok now.

and no i am not a lawyer, i refine petrol , much worse…
