Вы находитесь на странице: 1из 3

Programación en Excel

'source: https://www.devhut.net/2010/06/22/vba-converting-between-
decimal-and-binary/

'Decimal To Binary
' =================
' Source:
http://groups.google.ca/group/comp.lang.visual.basic/browse_thread/thread
/28affecddaca98b4/979c5e918fad7e63
' Author: Randy Birch (MVP Visual Basic)
' NOTE: You can limit the size of the returned
' answer by specifying the number of bits
Function Dec2Bin(ByVal DecimalIn As Variant, _
Optional NumberOfBits As Variant) As String
Dec2Bin = ""
DecimalIn = Int(CDec(DecimalIn))
Do While DecimalIn <> 0
Dec2Bin = Format$(DecimalIn - 2 * Int(DecimalIn / 2)) & Dec2Bin
DecimalIn = Int(DecimalIn / 2)
Loop
If Not IsMissing(NumberOfBits) Then
If Len(Dec2Bin) > NumberOfBits Then
Dec2Bin = "Error - Number exceeds specified bit size"
Else
Dec2Bin = Right$(String$(NumberOfBits, _
"0") & Dec2Bin, NumberOfBits)
End If
End If
End Function

'Binary To Decimal
' =================
Function Bin2Dec(BinaryString As String) As Variant
Dim X As Integer
For X = 0 To Len(BinaryString) - 1
Bin2Dec = CDec(Bin2Dec) + Val(Mid(BinaryString, _
Len(BinaryString) - X, 1)) * 2 ^ X
Next
End Function

'source: http://www.vbgold.com/vb-projects/integer-to-binary.shtml

Function IntToBin(ByVal IntegerNumber As Long)

IntNum = IntegerNumber
Do
'Use the Mod operator to get the current binary digit from
the
'Integer number
TempValue = IntNum Mod 2
BinValue = CStr(TempValue) + BinValue

'Divide the current number by 2 and get the integer result


IntNum = IntNum \ 2
Loop Until IntNum = 0

IntToBin = BinValue

End Function

Function BinToInt(ByVal BinaryNumber As String)

'Get the length of the binary string


Length = Len(BinaryNumber)

'Convert each binary digit to its corresponding integer value


'and add the value to the previous sum
'The string is parsed from the right (LSB - Least Significant
Bit)
'to the left (MSB - Most Significant Bit)
For x = 1 To Length
TempValue = TempValue + Val(Mid(BinaryNumber, Length - x +
1, 1)) * 2 ^ (x - 1)
Next

BinToInt = TempValue

End Function

Sub Test2()
Dim strSentence As String
Dim lngCount As Long
Dim strArray() As String

strSentence = "This is a string"


strSentence = StrConv(strSentence, vbUnicode)

strArray = Split(strSentence, vbNullChar)

For lngCount = 0 To UBound(strArray)


Debug.Print strArray(lngCount)
Next lngCount
End Sub
'source: https://stackoverflow.com/questions/30194208/is-a-string-an-
array-in-vba-that-can-be-iterated-through

Sub test()
Dim strSentence As String
Dim lngCount As Long

strSentence = "This is a string"

For lngCount = 1 To Len(strSentence)


Debug.Print Mid(strSentence, lngCount, 1)
Next lngCount
End Sub

'VBA

'source: https://stackoverflow.com/questions/53170591/convert-an-
alphanumeric-string-into-an-array-of-numbers-in-vba

Function StringToIntArray(str As String) As String


Dim v, w, s As String

v = Split(str)
For Each w In v
If IsNumeric(w) Then
s = s & "," & w
End If
Next w

StringToIntArray = "[" & Mid(s, 2) & "]"

End Function

Вам также может понравиться