Código fuente de 'Convertidor decimal-binario.asp'
<html>
<head>
<title>Convertidor decimal-binario - Códigos asp, programacion asp, descargas asp, rutinas asp</title>
</head>
<body style="font-family: Arial; font-size: 11pt">
<center><b><font size="3" face="Arial">Convertidor decimal-binario</font></b>
</center><br>
<form method="POST" action="Convertidor%20decimal-binario.asp" webbot-action="--WEBBOT-SELF--">
<p><b>Introduce un número en decimal: </b>
<input type="text" name="num" size="19">
<input type="submit" value="Enviar" name="B1">
<input type="reset" value="Borrar" name="B2"></p>
</form>
<%
'Converted from:
'http://www.planet-source-code.com/xq/ASP/txtCodeId.5320/lngWId.1/qx/vb/scripts/ShowCode.htm
num = request("num")
if num = "" then num = 255
if not(isNumeric(num)) then num = 10
Binary = Base(2, num, True)
Decimal = Dec(2, Binary)
response.write "Número en decimal =" & Decimal & "<br />"
response.write "Conversión a binario = " & binary & "<br />"
Function Base(BaseNum , Number , ClipZeros )
Dim i , MB , endstr
If BaseNum > 9 Or BaseNum < 2 Then Exit Function 'Filter out "bad" numbers
MB = MaxBit(BaseNum) 'Get the maximum amount of bits possible
'I know, this isn't needed... But it makes me feel secure :)
endstr = ""
If MB = 0 Then Exit Function 'This also makes me feel secure
For i = 1 To MB 'You know this
If BaseNum ^ (MB - i) <= Number Then 'If I can Get one of the BaseNum ^ (MB - i)'s out of Number
endstr = endstr & Int(Number / (BaseNum ^ (MB - i))) 'This will see how many BaseNum things are in Number, and put them in as a digit On the end String
Number = Number - (Int(Number / (BaseNum ^ (MB - i))) * (BaseNum ^ (MB - i))) 'This will subtract everything that was put in the End String
Else 'This is If Number fails its test
endstr = endstr & "0" 'Add a 0, needed If you are going To have accuracy in here
End If 'Comments on every line, live With it
Next 'Loop the i
'If we need To clip off the 0's at the start
If ClipZeros = True Then
'When there is a zero In front...
Do While Mid(endstr, 1, 1) = "0"
'Take it off...
endstr = Mid(endstr, 2, Len(endstr) - 1)
'And check again
Loop
'I don't know what To put here, sorry
End If
'Return the number String To the Function
Base = endstr
'End the function, what else?
End Function
Function Dec(OldBaseNum , Number )
Dim i , MB , endstr
If OldBaseNum > 9 Or OldBaseNum < 2 Then Exit Function 'Make sure the numbers are In the right area
MB = MaxBit(OldBaseNum) 'Get the maximum possible bits without blowing up vb
Do While Len(Number) < MB 'As Long as the number doesn't have all of the extra 0's...
Number = "0" & Number 'Add another...
Loop 'And check again
For i = 1 To MB 'What am I supposed to put? Sorry, I'll be serious now, just bored.
if endstr > 0 then
endstr = clng(endstr) + (OldBaseNum ^ (MB - i) * Mid(Number, i, 1)) 'This will see how much Each bit is worth, and multiply it by the actual value of it
else
endstr = (OldBaseNum ^ (MB - i) * Mid(Number, i, 1)) 'This will see how much Each bit is worth, and multiply it by the actual value of it
end if
Next 'Bleah
Dec = clng(endstr) 'This will return the number To the Function
End Function 'End the function
Function MaxBit(BaseNum )
Dim i , MB , buffer
'On Error Goto GotNum 'This is needed, you'll see why
'I like To Do that
MB = 0
For i = 1 To 20 'Start the i "loop"
buffer = BaseNum ^ i
MB = MB + 1 'This adds To the exponent
Next 'Loops the i
'GotNum: 'This is where it goes when it reaches the max bits possible
MaxBit = MB 'This will just return the value To the function, and send it over to the other 2
End Function 'End the function, duh
%>
</body></html>