Signed hex numbers

alanharper

New Member
Joined
Mar 30, 2014
Messages
14
I teach computer forensics classes and we have the need for a formula that will determine if any length hex number (typically 2,3, or 4 bytes) is negative and, if so, convert that number to its negative decimal equivalent. I have been able to do that with the following formula (cell E5 contains the negative hex number F90825):

=-(HEX2DEC("FFFFFF")-HEX2DEC(E5)+1)

If E5 contains F90825 then this formula will return the correct decimal value of -456,667. This formula works because it calculates the 15's complement of the number, adds 1 and then negates the result. If the formula is used on a hex value that is less than three bytes, it fails to return the correct number. For example, if E5 contained the value 9B06, the formula would return the value -16,737,530. However, if I adjust the number of F's to match the number of bytes in the hex number, the formula works. That seems to be the key to making this formula work.

I now need a modified formula or process to look at the hex number, determine the number of bytes (I think LEN can do that) and then adjust the formula to include the same number F's as there are hex digits in the number. I know I need an IF statement and some way to create a formula on the fly. A simple CASE statement from my programming days would work, but I did not see anything like that in Excel.

It also interesting to note that if these calculations are performed on the Windows calculator in Programming mode, you can simply enter the number and NOT it. This immediately calculates the 15's complement. The NOT function in Excel is a Boolean operator so it doesn't work. I think perhaps MOD would work in Excel, but I am not sure how to create the formula.

I hope that I have explained the problem well enough that someone out there can help.

Regards ... Alan Harper
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I don't think you can just look at a Hex number and know if it is positive or negative... I think you need to know the number of bits for the range of numbers you are looking at. The way to determine if an N-bit signed number is negative or positive is by looking at the number in binary (zero-filled to N bits if needed) and seeing if its leading bit is a 0 or 1... if it is 0, the number is positive and, if it is 1, the number is negative. Just looking at Hex number, in isolation, would mean nothing sign-wise as far as I know. By the way, if I remember correctly, for a signed Hex number, filled out to the proper bit-level, if the lead Hex number is between 0 an 7, the number is positive and, if it is between 8 and F, the number is negative.
 
Upvote 0
Thanks for the reply. I know that the number I need to convert is either positive or negative based on the leading hex digit as you stated as these numbers come from a $80 attribute in an MFT FILE record. I know the numbers are signed. What I need is a formula to do what I asked.
 
Upvote 0
There's no way to know unless you also know the bit length the format (unless by understanding any leading zeros are included, and the format is known to be a multiple of four bits) and the encoding. In VBA, Currency, Long, and Integer are all signed twos-complement; Byte is not. In other languages, there are signed and unsigned integers of varying lengths. The mantissas of Singles and Doubles are stored as sign and magnitude, and their exponents as unsigned with offsets.
 
Last edited:
Upvote 0
The formula that martindwilson suggested works for the negative hex numbers. Now I need to generalize it so it works with any signed hex number. I think an IF statement is needed but I don't know how to tell if the left most digit of the hex number is 8-F.
 
Upvote 0
The formula that martindwilson suggested works for the negative hex numbers. Now I need to generalize it so it works with any signed hex number. I think an IF statement is needed but I don't know how to tell if the left most digit of the hex number is 8-F.

Give this formula a try...

=HEX2DEC(E5)-IF(ISERR(FIND(LEFT(E5),"01234567")),16^LEN(E5),0)
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top