2015-01-25 1 views
0

У меня есть ploblem с преобразованием десятичных чисел в двоичный. Я хочу иметь пространство между 8b, но я не знаю, как это сделать в коде.IP-калькулятор. Десятичное значение в двоичном формате

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 


'----------------------------------------------------------- 
+0

«Я хочу иметь пространство между 8b» - что такое «8b»? –

+0

пробел между каждыми 8 числами. 8b = 8 байт – kamelija77

+0

Я разместил здесь свой код. Пожалуйста, разрешите проблему, потому что я действительно не знаю. я havent работал с визуальным базовым :) – kamelija77

ответ

-1
'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 "& 

гр & vbcrlf & "х" & общая & vbcrlf & cvcrlf

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 
+0

Это не ответ. Если вы хотите включить код с вашим вопросом, тогда отредактируйте сам вопрос. –

1

Быстрых и очень грязное:

Добавлять пробел после каждого октета:

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< 
+0

Я пробовал и его невозможно – kamelija77

+0

@ kamelija77 - ничего невозможного - см. доказательство –

Смежные вопросы