How to convert 14 bit Hex to Decimal - excel

I need to be able to convert 047C1BEA3A2480 into Decimal. This should convert to 1262359242482816. I have a large amount of hex numbers that need converting so would need a formula or VB script.
I have tried some things including a VB Module, however with this I need to prefix the number with 0X but then gives me a decimal number that is out by 4.
Any ideas would be great.

Use the inbuilt CDec function
Debug.Print CDec("&H" & "047C1BEA3A2480")
This will give you 1262359242482816
Screenshot

Split into 2 7-digits and use the HEX2DEC function. Unfortunately, Excel cannot handle that big a number, so it is rounding when you put it back in Excel. For exmaple, try to paste the decimal version into Excel. But if you want the formula anyway.
=HEX2DEC(LEFT(A4,7))*16^7+HEX2DEC(RIGHT(A4,7))
Otherwise, use Siddarth's solution and put it in Excel as Text.

Related

How to use =LEFT and =LEN function with scientific notation in Excel VBA?

I'm trying to write an Excel macro using VBA that will return only the first 5 numbers in a cell when the length of that cell exceeds 20. The field normally returns 15-digit alphanumeric results (which I need to leave alone) but in certain exceptions will return a 5-digit number with a multitude of zeroes following it (1234500000000000000000000...) which Excel converts into scientific notation (1.2345E+160). I am able to convert the cells to numbers instead of scientific notation and view the whole number.
I've tried to use code such as =IF(LEN(A1)>20,LEFT(A1,5),A1) and it just returns 1.2345E+160. Even though the whole number is displaying, Excel still thinks the cell length is 11 and won't display the first 5 digits.
I've also tried lines such as =IF(A1="E",LEFT(A1,6),A1) thinking it would detect the E, return 1.2345, and I could just remove the decimal points, but that didn't work either (it just returns the original 1.2345E+160).
I got the same results whether the cell was formatted as number or text. Is there a way around this?
Thank you for your time!
You are trying to use string manipulation on a number. Instead use math:
=A1/1E+160
If you do actually want to treat this thing as text, understand that the underlying value being stored is your 12345000000000000... and there is no decimal point in that thing. So you'll have to convert to text and add the decimal:
=LEFT(TEXT(A1,"0"), 1) & "." & MID(TEXT(A1,"0"), 2, 4)
But that's pretty ugly. I would just stick with math.

How to avoid scientific notation conversion in excel

I want to convert a hex number in excel to two 4 character hex values.
My approach is:
1. I am using TEXT function to convert hex number to 8-character hex.
2. Then, use MID function to extract first 4 characters and other 4 characters.
This approach works fine for most of the cases. However, I have come across a particular scenario, in which, it is failing.
For example,
My hex value is 62823E4. I wanted it to convert to 062823E4. However, internally excel is considering E4 as 10^4 (scientific notation). Formula used is shown in pic above.
Kindly help. Thanks in advance.
The TEXT function operates on numbers, so it will interpret hex data as decimal numbers.
Take a look at HEX2DEC and DEC2HEX functions. If you start with hex strings, you should first extract their values with HEX2DEC. Then, using DEC2HEX you can restore the hex string with the required number of digits.
A1='62823E4
A2=HEX2DEC(A1)
A3=DEX2HEX(A2;8)
A5=LEFT(A3;4)
A6=RIGHT(A3;4)

Extending a binary number to 32 bits in text

I have this number
111100000000000010001000
I want to extend it to 32 bits with leading zeros. In other words:
00000000111100000000000010001000
So I found this suggestion here:
Add leading zeroes/0's to existing Excel values to certain length
is to use the Right function. So I do:
=RIGHT("00000000000000000000000000000000"+A1,32)
I end up getting a number in Engineering notation. So as suggested somewhere else I add:
=TEXT(RIGHT("00000000000000000000000000000000"+A1,32), "0")
I still get
111100000000000000000000
Not 32-bit and the trailing 10001000 has become zeros.
Any idea what's happening here??
Excel takes that as a decimal number, not a binary number.
111100000000000010001000 as a decimal number is too much for the number precision Excel has to offer, so that is rounded to 111100000000000000000000 before you apply your zeros (which you can see yourself if you apply a numeric format to A1 that disallows scientific notation).
The solution is the same, treat all numbers as string. Prefix the source number in A1 with an apostrophe to make it a string, the RIGHT will then work as you expect.
Well, it actually won't, because I used + when I should have used &, so Excel will try to convert to numbers and actually make the addition. So correct the formula:
=RIGHT("00000000000000000000000000000000"&A1,32)

How to convert "Double" to formatted string in VBScript

I was working on small script in VB.
I need to format a double number which is result of division.
Thus there could be many digits after decimal places. I need to convert it to String with only two decimals places.
I used to do it in C# with Double.ToString("0.##");
is there any methode like that in VBScript ..
Please help ..
Himanshu
You're going to want the FormatNumber function. FormatNumber(num, 2) will give you the number formatted to 2 decimal points. See the link for more details.
http://www.w3schools.com/vbscript/func_formatnumber.asp

convert negative decimal number into HEX

I must admit I don't remember much about HEX and so on from school (25 years ago). In any case, I have some values in decimal format which I need to convert into HEX. I am using Excel but I could write a function in VBA if necessary (or do it by code in VB.NET).
I already know how the HEX-result should look like (another source) but I need to use Excel to get this result exactly. The source of decimal input and also the result of the (right) HEX result is from a Linux-system if that is important to know.
Positive numbers seem to be converted correctly while negative numbers give me an headache in the sense that Excel adds in the beinning of the HEX two additional letters (two FF) compared to result I want.
Example:
Decimal input: -524288
Correct HEX-output I must obtain: FFF80000
Using formula in Excel I get: FFFFF80000
(I get 2 FF extra in the beginning of the HEX-output)
Another example:
Decimal Input: -29446758
should be FE3EAD9A
but in Excel I get FFFE3EAD9A
It seems like I always get 2 extra FF in the HEX-output.
Can someone explain (in an easy way) why I get the 2 extra FF and if I can safely remove them?
In Excel, =DEC2HEX by default returns 10 characters.
If you want to get just 8, as your question suggest use:
=DEC2HEX(A1,8)
Nevertheless, unless you have a compatibility issue, you may left the default numbers. Remember that the "F" char acts for negative numbers as a padding char (the same way "0" is for positive numbers).
Edit
The above fails for negatives, as you stated in your comment.
The following works:
=RIGHT(DEC2HEX(A1),8)
I'm not quite sure what you are doing because you haven't included your formula. My guess is that you are using a function like this:
=DEC2HEX(A1)
Although it has an optional parameters to control how many digits are returned, that doesn't work when the input is negative.
Instead you should use some VBA:
Public Function DecToHex(val As Variant) As Variant
DecToHex = Hex(val)
End Function

Resources