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
 
Okay, if that is what you want, here is a more compact formula (two less function calls, so it should be more efficient) that you can use..

=HEX2DEC(E5)-16^LEN(E5)

Thanks Rick. That worked as well. Now I need to generalize it so that it works for all signed hex numbers. This formula works for negative hex numbers but not for positive hex numbers. I think I need to determine if the first digit is 0-7. I need GREP but I don't know how, or it, I can do GREP expressions inside an Excel formula.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Thanks Rick. That worked as well. Now I need to generalize it so that it works for all signed hex numbers. This formula works for negative hex numbers but not for positive hex numbers. I think I need to determine if the first digit is 0-7. I need GREP but I don't know how, or it, I can do GREP expressions inside an Excel formula.
I think our messages crossed in the ether... see Message #10 (refresh your view if necessary).
 
Upvote 0

Forum statistics

Threads
1,223,104
Messages
6,170,125
Members
452,303
Latest member
c4cstore

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