Vbscript IP计算器。十进制到二进制
我在把十进制数转换成二进制数方面遇到了问题。我希望8b之间有空格,但我不知道如何在代码中做到这一点Vbscript IP计算器。十进制到二进制,vbscript,Vbscript,我在把十进制数转换成二进制数方面遇到了问题。我希望8b之间有空格,但我不知道如何在代码中做到这一点 Function str2bin(strAddress) 'special decimal to binary function 'input 4 octet ip address 'output 32bit binary number objAddress = Split(strAddress, ".") For Each strOctet In objAddress
Function str2bin(strAddress)
'special decimal to binary function
'input 4 octet ip address
'output 32bit binary number
objAddress = Split(strAddress, ".")
For Each strOctet In objAddress
intOctet = CInt (strOctet)
strOctBin = ""
For x = 1 To 8
If intOctet Mod 2 > 0 Then
strOctBin = "1" & strOctBin
Else
strOctBin = "0" & strOctBin
End If
intOctet = Int(intOctet / 2)
Next
str2bin = str2bin & strOctBin
Next
End Function
'-----------------------------------------------------------
又快又脏:
在每个八位字节后追加一个空格:
str2bin = str2bin & strOctBin & " "
和Trim()
调用代码中的返回值,以除去尾随空格
证据:
type 28139908.vbs & cscript 28139908.vbs
Option Explicit
Function str2bin(strAddress)
'special decimal to binary function
'input 4 octet ip address
'output 32bit binary number
Dim objAddress, strOctet, intOctet, strOctBin, x
objAddress = Split(strAddress, ".")
For Each strOctet In objAddress
intOctet = CInt (strOctet)
strOctBin = ""
For x = 1 To 8
If intOctet Mod 2 > 0 Then
strOctBin = "1" & strOctBin
Else
strOctBin = "0" & strOctBin
End If
intOctet = Int(intOctet / 2)
Next
str2bin = str2bin & strOctBin & " "
Next
End Function
Dim sIP : sIP = "127.15.32.255"
WScript.Echo sIP, ">" & str2bin(sIP) & "<", ">" & Trim(str2bin(sIP)) & "<"
127.15.32.255 >01111111 00001111 00100000 11111111 < >01111111 00001111 00100000 11111111<
键入28139908.vbs&cscript 28139908.vbs
选项显式
功能str2bin(strAddress)
'特殊的十进制到二进制函数
'输入4个八位字节的ip地址
'输出32位二进制数
Dim objAddress,strOctet,intOctet,strOctBin,x
objAddress=拆分(以“.”号填列)
对于objAddress中的每个strOctet
intOctet=CInt(strOctet)
strOctBin=“”
对于x=1到8
如果INTOCET Mod 2>0,则
strOctBin=“1”&strOctBin
其他的
strOctBin=“0”&strOctBin
如果结束
intocet=Int(intocet/2)
下一个
str2bin=str2bin&strOctBin&“
下一个
端函数
Dim sIP:sIP=“127.15.32.255”
WScript.Echo sIP“>”&str2bin(sIP)&“&Trim(str2bin(sIP))&”
“我希望8b之间有空格”--什么是“8b”?每个8个数字之间的空格。8b=8 byteI已将我的代码放在这里。请解决问题,因为我真的不知道。我还没有使用visual basic:)@kamelija77-没有什么是不可能的-请参阅证据这不是答案。如果您想在问题中包含代码,请编辑问题本身。
'Subnet Calculator V1
'Script by Chavdarova
'usage cscript VBScriptSubnetCalcV2 > output.txt
'force script to run in cscript mode
Set objShell = CreateObject("WScript.Shell")
If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
objShell.Run "%comspec% /k cscript //nologo """ & WScript.ScriptFullName & """", 1, False
WScript.Quit
End If
'a bit of test code to feed a range of addresses into script for testing
' snm="255.255.192.0"
' for iCount=0 to 255
' ip="172.16.17."&iCount
' CalcSubnet ip, snm
' next
strIP=inputbox("Vnesite IP", "Chavdarova Subnet Calc", "172.16.98.53")
strSN=inputbox("Vnesite Subnet Mask", "Chavdarova Subnet Calc", "255.255.224.0")
CalcSubnet strIP, strSN
wscript.quit
----------
function CalcSubnet(strIP,strSNM)
binSNM=str2bin(strSNM)
binIP=str2bin(strIP)
'IP <AND> SN to find Network addresses
for c=32 to 1 step -1
temp=(cint(mid(binIP,c, 1)) and cint(mid(binSNM,c, 1))) & temp
next
netwAdd=temp : temp=""
'IP <OR> SN to find blocks of all "ones" - these addresss are broadcast addresses
for c=32 to 1 step -1
temp=(cint(mid(binIP,c, 1)) or cint(mid(binSNM,c, 1))) & temp
next
bcCheck=temp : temp=""
'Calc 1st. host address in range (Network Address + 1)
ist=binAdd(netwAdd,string(31, "0")&"1")
'Calc Last host address in range (111111...1 - bcCheck + IP - 0...000001)
lst=binSub(string(32,"1"),bcCheck)
lst=binadd(lst,binIP)
lst=binsub(lst,string(31, "0")&"1" )
wscript.echo "IP "&binIP&" "&bin2str(binIP)&vbcrlf&_
"Subnet Mask "&binSNM&" "&bin2str(binSNM)&vbcrlf&_
"Subnet Network "&netwAdd&" "&bin2str(netwAdd)&vbcrlf&_
"Host min "&ist &" "& bin2str(ist) &vbcrlf&_
"Host max "&lst &" "& bin2str(lst) &vbcrlf&_
"Hosts "&binsub(lst,ist) &" "& Bin2Dec(binsub(lst,ist)) &vbcrlf
end function
----------
Function Bin2Dec(strBin)
'Plain old binary to decimal function
result = 0
for intIndex = len(strBin) to 1 step -1
strDigit = mid(strBin, intIndex, 1)
if strDigit = "0" then
'do nothing
elseif strDigit = "1" then
result = result + (2 ^ (len(strBin)-intIndex))
else
Bin2Dec = 0
exit for
end if
next
Bin2Dec = result
End Function
----------
Function bin2str(strBinary)
'special binary to decimal function
'input 32bit binary number
'output 4 octet ip address
For iPosOct = 1 To 4
strOctBin = Right(Left(strBinary, iPosOct * 8), 8)
intOctet = 0
intValue = 1
For iPosBin = 1 To Len(strOctBin)
If Left(Right(strOctBin, iPosBin), 1) = "1" Then
intOctet = intOctet + intValue
end if
intValue = intValue * 2
Next
If bin2str = Empty Then
bin2str = CStr(intOctet)
Else
bin2str = bin2str & "." & CStr(intOctet)
end if
Next
End Function
----------
Function str2bin(strAddress)
'special decimal to binary function
'input 4 octet ip address
'output 32bit binary number
objAddress = Split(strAddress, ".")
For Each strOctet In objAddress
intOctet = CInt (strOctet)
strOctBin = ""
For x = 1 To 8
If intOctet Mod 2 > 0 Then
strOctBin = "1" & strOctBin
Else
strOctBin = "0" & strOctBin
End If
intOctet = Int(intOctet / 2)
Next
str2bin = str2bin & strOctBin & " "
Next
End Function
----------
function binSub(binA,binB)
'subtract one 32bit binary number from another
'binA must be biggest
c=0
for i=32 to 1 step-1
a=cint(mid(binA,i,1))
b=cint(mid(binB,i,1))
if a=0 and b=0 and c=0 then
subt=0 : c=0
elseif a=1 and b=0 and c=0 then
subt=1 : c=0
elseif a=0 and b=1 and c=0 then
subt=1 : c=1
elseif a=1 and b=1 and c=0 then
subt=0 : c=0
elseif a=1 and b=1 and c=1 then
subt=1 : c=1
elseif a=1 and b=0 and c=1 then
subt=0 : c=0
elseif a=0 and b=1 and c=1 then
subt=0 : c=0
elseif a=0 and b=0 and c=1 then
subt=1 : c=1
else
msgbox "This function is only for subtracting 2 32bit binary numbers"
binSub=0 : exit function
end if
total=subt&total
'wscript.echo "a-"&BinA&" "&a&vbcrlf&"b-"&BinB&" "&b&vbcrlf&"subtraction "&subt&vbcrlf&"carry "&
next
if c=1 then
msgbox "Error you are subtracting a larger number from a smaller number"&vbcrlf&binA&vbcrlf&binB
end if
binsub=total
end function
----------
function binAdd(binA,binB)
'add two 32bit binary numbers together
c=0
for i=32 to 1 step-1
a=cint(mid(binA,i,1))
b=cint(mid(binB,i,1))
if a=0 and b=0 and c=0 then
add=0 : c=0
elseif a=1 and b=0 and c=0 then
add=1 : c=0
elseif a=0 and b=1 and c=0 then
add=1 : c=0
elseif a=1 and b=1 and c=0 then
add=0 : c=1
elseif a=1 and b=1 and c=1 then
add=1 : c=1
elseif a=1 and b=0 and c=1 then
add=0 : c=1
elseif a=0 and b=1 and c=1 then
add=0 : c=1
elseif a=0 and b=0 and c=1 then
add=1 : c=0
else
msgbox "Error this function is only for adding 2 32bit binary numbers together"
binAdd=0 : exit function
end if
total=add&total
'wscript.echo "a-"&BinA&" "&a&vbcrlf&"b-"&BinB&" "&b&vbcrlf&"addition "&add&vbcrlf&"carry "& c&vbcrlf&"x-"&total
next
binAdd=total
end function