[英]How to Convert Binary String into Array Of Integer Bit using Excel VBA
我在使用Excel宏編碼解決此問題時遇到了困難。 我的輸入數據是二進制字符串'1110'。 需要提取二進制字符串並按順序將二進制的每一位返回到數組中(作為整數位)。 請幫助我...非常感謝教練的幫助。
對於每個位= 1,該位的數組值= 2的冪。最后,將所有值加起來。 例:
輸入= 1110(二進制字符串)存儲到數組(i)中。 i = 3(輸入的總位= 4)
Array (0) = 1^2 to the pwr of 0 = 1
Array (1) = 1^2 to the pwr of 1 = 2
Array (2) = 1^2 to the pwr of 2 = 4
Array (3) = 1^2 to the pwr of 3 = 8
返回的最終輸出是所有數組列表的總和,在本例中為15
從我的頭頂開始,無需測試,這將使您接近。
Dim bin as String
bin="1101"
Dim bits(Len(bin)) as Integer
Dim bitIndex as Integer
Dim sum as Integer
sum = 0
For bitIndex = 0 to Len(bin)-1
bits(bitIndex) = CInt(Mid(bin,bitIndex+1, 1)) * (2 ^ bitIndex)
Debug.Print bits(bitIndex)
sum = sum + bits(bitIndex)
Next
Debug.Print sum
您可以使用內置的Excel函數BIN2DEC(從分析工具庫)獲得最終答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.