Extending a binary number to 32 bits in text - excel

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)

Related

How to remove all decimal points, but retain value In Microsoft Excel?

I need to display with no decimal, but retaining the numbers that
appear after the last decimal. For example, given `03.1037.190
I tried roundup and trunct but not sure how it works.
Try this on a string where . could be on a dynamic spot
=SUBSTITUTE(A1,".","",LEN(A1)-LEN(SUBSTITUTE(A1,".","")))
Or when your string always follows the same pattern ##.####.####.## you could try:
=REPLACE(A1,13,1,"")
The question is quite unclear as you have a value with the same thousand and decimal delimiter and also 4 numbers between the delimiters. It would help a lot if you specified the actual number without thousand delimiters.
Assuming there are no decimals (31.037.190.301): remove "." using:
=SUBSTITUTE(A1,".","")
and Excel will recognize it as a number

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.

str(1) returning with a space in front excel vba

In an excel program, I was debugging when I noticed that the string function of a number returned the number but with a space in front of it: str(1) returned " 1".
As far as I am aware, there is nothing much on the internet about this, so I would appreciate knowing whether it is a glitch, or if I am doing something wrong. There is a way of working around, if this may help:
Mid(Str(1), 2, 1) So what this does is take the second character of " 1", and if the number is 5 characters long, for example, change the number at the end to 5 eg Mid(Str(11111), 2, 5).
Thanks for any feedback, however, with the work-around, it is not 100% necessary that this question is answered.
If it helps, I use office 365...
From the VBA Reference:
When numbers are converted to strings, a leading space is always reserved for the sign of number. If number is positive, the returned string contains a leading space and the plus sign is implied. Use the Format function to convert numeric values you want formatted as dates, times, or currency or in other user-defined formats. Unlike Str, the Format function doesn't include a leading space for the sign of number.
Please see:
Str Function
The leading space provided by VBA's Str function to positive numbers is for alignment purposes.
Text/strings are by default left-aligned on the worksheet. Since negative numbers receive a hyphen, positive numbers receive a space to maintain alignment.
Btw, if you omit VBA's Mid function's third argument (number of characters), all remaining characters from the second argument's starting position are returned. This means that Mid(Str(<any number>), 2) will always return the full number although any negative number's sign would be omitted. This does not work with the worksheet's MID function.

Parsing apart a decimal into two integers in either Stata or Excel

I'm working with a dataset that has really terrible ID numbers that are an integer followed by a 13 digit decimal. However, the first 6-7 decimal places are zeroes. For example:
10.0000000960554
This is making my life difficult. So I want to parse the IDs apart at the decimal into two integers, drop the leading zeros, and put them back together as one giant integer. However, everything I find for how to do this in Excel keeps the numbers after the decimal after the decimal. For Stata, I've tried to convert the numeric into a string so I can then parse it, but Stata won't let me because it's a decimal:
encode ScrambledID, generate StringID
Here's the error:
not possible with numeric variable
r(107);
An added issue, I can't just split the decimal in Excel and then multiply by 1e+12 because it messes with the values (long story with how they were derived).
Like I said, I'm fine with doing this in either Stata or Excel. Either way this is driving me nuts.
In Excel:
In one column put:
=int(A1)
In the next put:
=--MID(A1,FIND(".",A1)+1,999)
As #Grade'Eh'Bacon stated, I have use a few shortcuts in the above formula. The -- at the beginning change text that are numbers into numbers. It replaces the VALUE() function.
The 999 is a superfluous number in that it is assumed the length of the string being split is not longer than 999 characters. It can be replaced with the LEN() function which would return the actual length of the string.
So putting the two together:
=VALUE(MID(A1,FIND(".",A1)+1,LEN(A1))
Where A1 is the location of the number
Your story is truly shocking.
I'd advise extreme caution in any software. For a start, numbers with decimal parts will be rendered differently depending on whether they are imported as 4-byte or 8-byte reals, in Stata terms as floats or doubles. The underlying problem is that many decimal numbers have no exact binary representation.
In Stata terms, encode is indeed out of the question for a numeric variable (and your example would also fail for other reasons). But ideally you should import the identifiers as strings in the first place. Otherwise you should try a conversion such as generate stringID = string(numid, "%16.13f").
. di %21s string(10.0000000960554, "%16.13f")
10.0000000960554
. di %21s string(10.00000009605539, "%16.13f")
10.0000000960554
. di %21s string(10.00000009605544, "%16.13f")
10.0000000960554
. di %21s string(10.00000009605535, "%16.13f")
10.0000000960554

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