Decimal to binary conversion for large numbers in Excel

后端 未结 12 2116
长情又很酷
长情又很酷 2020-12-14 02:31

I have some large numbers in an Excel sheet and I want to convert them to binary.

e.g.

12345678  
965321458  
-12457896
12条回答
  •  攒了一身酷
    2020-12-14 02:56

    I just tried the formula above, and found that Microsoft screwed up the DEC2BIN function in another way that keeps the formula from working correctly with negative numbers. Internally, DEC2BIN uses a ten bit result; leading zeroes are dropped from the text result, unless the optional length parameter is used, in which case the required number of leading zeroes are left in the string. But here's the rub: a negative number always starts with a one, so there are no leading zeroes to drop, so DEC2BIN will always show all ten bits! Thus, DEC2BIN(-1,8), which should show 11111111 (eight ones) will instead show 1111111111 (ten ones.)

    To fix this, use RIGHT to trim each eight bit chunk to eight bits, dumb as that sounds.

    =RIGHT(DEC2BIN(QUOTIENT(A1,256^3),8),8) & RIGHT(...
    

    (I read through the VBA, and it does not have the same problem, but it doesn't look like it will handle negatives at all.)

提交回复
热议问题