gpt4 book ai didi

excel - 在excel vba中将字符串转换为二进制

转载 作者:行者123 更新时间:2023-12-04 20:26:26 26 4
gpt4 key购买 nike

这是我的代码,我尝试将字符串转换为二进制但它不起作用。请有人帮助我吗?过去5天我一直在努力解决这个问题!!
先感谢您!

  Sub Button1_Click()
Dim strText As String
strText = ActiveWorkbook.Sheets("Sheet2").Range("A1")

Debug.Print strText
Dim n, val As Integer
n = Len(strText)

Debug.Print n

Dim str As String
str = Mid(strText, 1, 1)
Debug.Print str
For i = 1 To n
'val = CInt(strarr(i))
val = Asc(str)

Debug.Print val
Dim bin As String
Dim modval As Integer
bin = ""

While val > 0
modval = val - (2 * (val \ 2))

If modval = 1 Then
bin = bin & "1"
val = 0
Else
bin = bin & "0"
val = 0
End If
Wend

bin = StrReverse(bin)
Debug.Print bin
Next i
Debug.Print i
ActiveWorkbook.Sheets("Sheet2").Range("A2") = bin
End Sub

最佳答案

Sub Button1_Click()
Dim fullstr As String
fullstr = ActiveWorkbook.Sheets("Sheet2").Range("A1")

Dim bin As String
bin = ""

Dim j As Long
For j = 1 To Len(fullstr)
Dim str As String
str = Mid$(fullstr, j, 1)

Dim z As Double
z = Asc(str)

Dim i As Long
For i = 7 To 0 Step -1
Dim y As Double
y = (2 ^ i)

bin = bin & Int(((z / y) - Int(((z / y) / 2)) * 2))
Next i
bin = bin & " "
Next j

ActiveWorkbook.Sheets("Sheet2").Range("A2") = bin
End Sub

关于excel - 在excel vba中将字符串转换为二进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59204567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com