How to do BINARY conversion using Command Line Only

Sometimes it is useful to be able to know the network address of a machine…I thought about how we could do this in a batch file. To get the network address requires knowing the IP address + Subnet of a machine.

To calculate the Network Address though requires converting IP & Subnet to binary, then whenever the subnet has a “1” use that value from the same location in the IP. Example below:

Decimal Binary
IP 192.168.1.1 11000000.10101000.00000001.00000001
Subnet 255.255.0.0 11111111.11111111.00000000.00000000
Network 192.168.0.0 11000000.10101000.00000000.00000000

Binary conversion is something typically not done in batch files (and for good reason) so I thought it would be interesting to try. Of course with multiple network adapters the result of this program may be a bit unpredictable…

This has function called GetNetworkAddress in which you can use CALL :GetNetworkAddress [ip address] [subnet] which will return a value !NetworkAddressDecimal! containing the network address.

Now looking at this you might think I have way too much time on my hands, well that’s not entirely true. I just avoid sleep whenever possible Smile

image

File can be downloaded here: http://tiange.com.au/GetNetworkAddress.zip

@echo off
REM Get Current Network Address of local/remote machine by Malcolm McCaffery

REM We need this so we can change variables during execution of program
SETLOCAL ENABLEDELAYEDEXPANSION

set /P HostName=Please type computer name:

REM Use WMI query to get IP address and subnet of computer
REM We use ‘DefaultIPGateway’ instead of ‘IPAddress’ because on Vista & later
REM IPAddress returns IPv4+IPv6 address, making processing a bit more complex
REM DefaultIPGateway will be an IPv4 address only
IF “%HostName%” EQU “” (
  FOR /F “skip=1 tokens=1,2” %%i IN (‘wmic nicconfig WHERE IPEnabled^=True GET DefaultIPGateway^,IPSubnet’) DO (
    SET CurrentIP=%%i
    SET CurrentSubnetMask=%%j
    Goto FoundIP
  )
) Else (
  FOR /F “skip=1 tokens=1,2” %%i IN (‘wmic /node:^’%HostName%^’ nicconfig WHERE DefaultIPGateway^=True GET IpAddress^,IPSubnet’) DO (
    SET CurrentIP=%%i
    SET CurrentSubnetMask=%%j
    Goto FoundIP
  )
)

:FoundIP
Rem Strip {” “} off the results from WMIC
SET CurrentIP=%CurrentIP:~2,-2%
SET CurrentSubnetMask=%CurrentSubnetMask:~2,-2%
echo IP=!CurrentIP! Subnet=!CurrentSubnetMask!

CALL :GetNetworkAddress %CurrentIP% %CurrentSubnetMask%
SET HostNetwork=!NetworkAddressDecimal!

echo Host Network: !HostNetwork!
Goto TheEnd

:GetNetworkAddress
Call :ConvertIPToBinary %1

SET HostIP=!BinaryIP!

Call :ConvertIPToBinary %2
SET Subnet=!BinaryIP!

SET C=0
SET NetworkAddressBinary=

SET A1=!HostIP:~0,1!
SET A2=!HostIP:~1,1!
SET A3=!HostIP:~2,1!
SET A4=!HostIP:~3,1!
SET A5=!HostIP:~4,1!
SET A6=!HostIP:~5,1!
SET A7=!HostIP:~6,1!
SET A8=!HostIP:~7,1!
SET A9=!HostIP:~8,1!
SET A10=!HostIP:~9,1!
SET A11=!HostIP:~10,1!
SET A12=!HostIP:~11,1!
SET A13=!HostIP:~12,1!
SET A14=!HostIP:~13,1!
SET A15=!HostIP:~14,1!
SET A16=!HostIP:~15,1!
SET A17=!HostIP:~16,1!
SET A18=!HostIP:~17,1!
SET A19=!HostIP:~18,1!
SET A20=!HostIP:~19,1!
SET A21=!HostIP:~20,1!
SET A22=!HostIP:~21,1!
SET A23=!HostIP:~22,1!
SET A24=!HostIP:~23,1!
SET A25=!HostIP:~24,1!
SET A26=!HostIP:~25,1!
SET A27=!HostIP:~26,1!
SET A28=!HostIP:~27,1!
SET A29=!HostIP:~28,1!
SET A30=!HostIP:~29,1!
SET A31=!HostIP:~30,1!
SET A32=!HostIP:~31,1!

SET B1=!Subnet:~0,1!
SET B2=!Subnet:~1,1!
SET B3=!Subnet:~2,1!
SET B4=!Subnet:~3,1!
SET B5=!Subnet:~4,1!
SET B6=!Subnet:~5,1!
SET B7=!Subnet:~6,1!
SET B8=!Subnet:~7,1!
SET B9=!Subnet:~8,1!
SET B10=!Subnet:~9,1!
SET B11=!Subnet:~10,1!
SET B12=!Subnet:~11,1!
SET B13=!Subnet:~12,1!
SET B14=!Subnet:~13,1!
SET B15=!Subnet:~14,1!
SET B16=!Subnet:~15,1!
SET B17=!Subnet:~16,1!
SET B18=!Subnet:~17,1!
SET B19=!Subnet:~18,1!
SET B20=!Subnet:~19,1!
SET B21=!Subnet:~20,1!
SET B22=!Subnet:~21,1!
SET B23=!Subnet:~22,1!
SET B24=!Subnet:~23,1!
SET B25=!Subnet:~24,1!
SET B26=!Subnet:~25,1!
SET B27=!Subnet:~26,1!
SET B28=!Subnet:~27,1!
SET B29=!Subnet:~28,1!
SET B30=!Subnet:~29,1!
SET B31=!Subnet:~30,1!
SET B32=!Subnet:~31,1!

:LoopC

SET /A C=!C!+1

IF “!B%C%!”==”1” (
  SET NetworkAddressBinary=!NetworkAddressBinary!!A%C%!
) ELSE (
  SET NetworkAddressBinary=!NetworkAddressBinary!0
)

IF !C! LSS 32 GOTO LoopC

Call :ConvertIPToDecimal !NetworkAddressBinary!
SET NetworkAddressDecimal=!DecimalIP!
Goto :EOF

:ConvertIPToBinary
SET InputIP=%1
SET BinaryIP=

FOR /F “tokens=1-4 delims=.” %%i IN (“%InputIp%”) DO (
  call :ConvertToBinary %%i
  set BinaryIP=!Output!
 
  call :ConvertToBinary %%j
  set BinaryIP=!BinaryIP!!Output!
 
  call :ConvertToBinary %%k
  set BinaryIP=!BinaryIP!!Output!
 
  call :ConvertToBinary %%l
  set BinaryIP=!BinaryIP!!Output!
)

Goto :EOF

:ConvertIPToDecimal
SET InputIP=%1
SET DecimalIP=

SET A1=!InputIP:~0,1!
SET A2=!InputIP:~1,1!
SET A3=!InputIP:~2,1!
SET A4=!InputIP:~3,1!
SET A5=!InputIP:~4,1!
SET A6=!InputIP:~5,1!
SET A7=!InputIP:~6,1!
SET A8=!InputIP:~7,1!
SET A9=!InputIP:~8,1!
SET A10=!InputIP:~9,1!
SET A11=!InputIP:~10,1!
SET A12=!InputIP:~11,1!
SET A13=!InputIP:~12,1!
SET A14=!InputIP:~13,1!
SET A15=!InputIP:~14,1!
SET A16=!InputIP:~15,1!
SET A17=!InputIP:~16,1!
SET A18=!InputIP:~17,1!
SET A19=!InputIP:~18,1!
SET A20=!InputIP:~19,1!
SET A21=!InputIP:~20,1!
SET A22=!InputIP:~21,1!
SET A23=!InputIP:~22,1!
SET A24=!InputIP:~23,1!
SET A25=!InputIP:~24,1!
SET A26=!InputIP:~25,1!
SET A27=!InputIP:~26,1!
SET A28=!InputIP:~27,1!
SET A29=!InputIP:~28,1!
SET A30=!InputIP:~29,1!
SET A31=!InputIP:~30,1!
SET A32=!InputIP:~31,1!

SET CurrentValue=128
SET CurrentCounter=0
SET C=0
:LoopD

SET /A C=!C!+1

IF “!A%C%!”==”1” SET /A CurrentCounter=!CurrentCounter!+!CurrentValue!

IF !CurrentValue! EQU 1 (
SET DecimalIP=!DecimalIP!!CurrentCounter!
SET CurrentCounter=0
SET CurrentValue=256
IF !C! LSS 32 SET DecimalIP=!DecimalIP!.
)

SET /A CurrentValue=!CurrentValue!/2

IF !C! LSS 32 GOTO LoopD

Goto :EOF

:ConvertToBinary

SET Output=
SET CurrentValue=128
SET InputValue=%1

:TopLoop
IF !CurrentValue! LEQ !InputValue! (
SET Output=!Output!1
SET /A InputValue=!InputValue!-!CurrentValue!
) ELSE (
SET Output=!Output!0

If !CurrentValue! EQU 1 GOTO :EOF
SET /A CurrentValue=!CurrentValue!/2

Goto TopLoop

:TheEnd
pause

About chentiangemalc

specializes in end-user computing technologies. disclaimer 1) use at your own risk. test any solution in your environment. if you do not understand the impact/consequences of what you're doing please stop, and ask advice from somebody who does. 2) views are my own at the time of posting and do not necessarily represent my current view or the view of my employer and family members/relatives. 3) over the years Microsoft/Citrix/VMWare have given me a few free shirts, pens, paper notebooks/etc. despite these gifts i will try to remain unbiased.
This entry was posted in Batch Files and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s