利用EXCEL VBA指定將十進位浮點數轉換為位元

來源:互聯網
上載者:User


1.EXCEL 內建的工具/載入宏/分析工具/ 
中的確有類似函數可將十進位數輕鬆轉化為二進位。可是該函數的致命缺點是只能裝換

-512~511之間的整數,超過此範圍的數則無能為力。

所以我們只能自己利用vba寫個可用的函數。


2.2.ALT+F11或
工具/宏/visual basic editor/
,插入模組 考入以下函數


Function N10to2(ByVal b As Long) As String  'This function was got from internet.

       Dim a As Long

       Dim a1 As String

       Dim s As String

     Do

        a = b Mod 2

            a1 = CStr(a)

        s = a1 & s

        b = b / 2

     Loop Until b = 0

     N10to2 = s

  End Function

 

Function DECto2(ByVal b, ByVal c As Long) As String  'wrote by myself

       Dim a1 As String

       Dim s As String

       Dim T As Double

       T = CDec(b)

     Do

        T = T * 2#

            a1 = CStr(T)

          If T > 1 Then

          T = T - 1

          s = s & "1"

          Else

          s = s & "0"

          End If

        c = c - 1

     Loop Until c <= 0

     DECto2 = s

  End Function


3.將浮點數拆成整數部分和小數部分。分別利用上面的兩個函數,前一個用於將整數部分轉化成二進位字串,後一個函數用於將小數部分轉化成二進位字串。


4。將符號,指數,尾數三個部分合成成一個完整的位元



 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.