Sunday, 23 January 2011

oh and here's the code (for excel)

This will take text input into cell b2 and spit out both the ASCI and binary for each character in turn.

Sub Conversion()

Dim AscVal As Integer
Dim i As Integer
Dim InputString As String
InputString = Range("b2")

For i = 1 To Len(Range("b2"))
PasteLocation = Len(Range("b2")) * 2 + 4


AscVal = Asc(Left(InputString, 1))
Range("B" & i + 3) = (AscVal)
Range("c" & i + 3) = IntToBin(AscVal)
'BinOut = BinOut & IntToBin(AscVal)
InputString = Right(InputString, Len(InputString) - 1)
Next i

End Sub
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

No comments:

Post a Comment