You are on page 1of 4

Public Function Dec2Bin(ByVal DecNum As Long) As String ' Thanks Thinker Select Case DecNum Case Is > 1 Dec2Bin

= Dec2Bin(DecNum \ 2) & CStr(DecNum And 1) Case Is > 0 Dec2Bin = "1" Case Else Dec2Bin = "0" End Select ' Dec2Bin = Right("0000000000000000" & Dec2Bin, 16) End Function ======================================================================= 'THIS MIGHT BE WORKING This function converts a decimal value to a string containing a binary respresen tation of the value. It is limited to a maximum value of 65536 (1111 1111 1111 1 111 in binary). This maximum can be changed by setting the intExp initial value. Function DecToBin(intDec) dim strResult dim intValue dim intExp strResult = "" intValue = intDEC intExp = 65536 while intExp >= 1 if intValue >= intExp then intValue = intValue - intExp strResult = strResult & "1" else strResult = strResult & "0" end if intExp = intExp / 2 wend DecToBin = strResult End Function ============================================================ Public Function DecToBin(DeciValue As Long, Optional NoOfBits As Integer = 8) _ As String '******************************************************************************* * '* Name : DecToBin '* Date : 2003 '* Author : Alex Etchells '******************************************************************************* ** Dim i As Integer 'make sure there are enough bits to contain the number Do While DeciValue > (2 ^ NoOfBits) - 1 NoOfBits = NoOfBits + 8 Loop

DecToBin = vbNullString 'build the string For i = 0 To (NoOfBits - 1) DecToBin = CStr((DeciValue And 2 ^ i) / 2 ^ i) & DecToBin Next i End Function ======================================================== 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 ===================================================== '************************************** ' Name: Convert decimal numbers to binar ' y string. Ultra fast! ' Description:This 'Ultra-fast' code con ' verts any decimal number to its binary e ' quivalent representation of 0's and 1's. ' For example, a decimal value 10 will con ' vert to 1010, and a value 500000 will co ' nvert to 1111010000100100000. The output ' binary value is a string representing th ' e binary number. Unlike any other conven ' tional method, this method is significan ' tly fast.....yet only 5 lines of code!. ' Try it. ' By: K. Mehdi ' ' ' Inputs:Any decimal number. ' ' Returns:Binary representation of the d ' ecimal number (in string format). ' 'Assumes:None ' 'Side Effects:None 'This code is copyrighted and has limite ' d warranties. 'Please see http://www.Planet-Source-Cod ' e.com/xq/ASP/txtCodeId.8237/lngWId.4/qx/ ' vb/scripts/ShowCode.htm

'for details. '************************************** 'Convert decimal number to binary string ' DecValue = 500000 'Your decimal number here Do TempValue = DecValue Mod 2 BinValue = CStr(TempValue) + BinValue DecValue = DecValue \ 2 Loop Until DecValue = 0 'Show result Response.Write BinValue ========================================== Sub Text1_Change () Dim i As Long, x As Long, bin As String Const maxpower = 30 ' Maximum number of binary digits supported. text1.MaxLength = 9 ' Maximum number of decimal digits allowed. text2.Enabled = False ' Prevent typing in second text box. bin = "" 'Build the desired binary number in this string, bin. x = Val(text1.Text) 'Convert decimal string in text1 to long integer If x > 2 ^ maxpower Then MsgBox "Number must be no larger than " & Str$(2 ^ maxpower) text2.Text = "" Exit Sub End If ' Here is the heart of the conversion from decimal to binary: ' Negative numbers have "1" in the 32nd left-most digit: If x < 0 Then bin = bin + "1" Else bin = bin + "0" For i = maxpower To 0 Step -1 If x And (2 ^ i) Then ' Use the logical "AND" operator. bin = bin + "1" Else bin = bin + "0" End If Next text2.Text = bin ' The bin string contains the binary number. End Sub ================================================= Private Function CBin(Number As Integer) As String Dim Temp As Variant Temp = 1 'Can't fouble nothing Do Until Temp > Number 'sets starting point for Len Temp = Temp * 2 Loop Do Until Temp < 1 If Number >= Temp Then CBin = CBin + "1"

Number = Number - Temp Else CBin = CBin + "0" End If Temp = Temp / 2 Loop 'Loop until string is complete CBin = CStr(Val(CBin)) End Function ====================================================

You might also like