converting ip addresses to binary

Can someone point me in the right direction? I'm trying to understand how to do this?

Also, how to find out what your subnet is???

You must be a bit more specific. An IPv4 address is a 32 bit word. If you have a look in the /usr/include/netinet/in.h header file you will see a struct in_addr, which has only one member of type in_addr_t. This is the generic structure used in most programmes in order to define an IP address. Also you will see in the in.h that the in_addr_t is defined as a uint32_t. What I am getting at is that when you define an IP address what you actually define is an integer of size 32 bits. You do not have to conver the ip address to a binary.
In order to find your subnet you should first do an ioctl with SIOCGIFNETMASK get the netmask and the do a logical and between the ip address and the netmask. The result will be your subnet.

You need to give us WAY more info on your system. Configuration files that tell you IP addresses and Subnet Mask depend on your Flavor of UNIX.

Typically, the IP address can be obtained from /etc/hosts file. In HPUX, the subnet mask is defined in the /etc/rc.config.d/netconf file. Most other flavors put this type of file under /etc, which is for all configuration files.

You can do a find to determine what file your IP address and Subnet mask are in. Start in the /etc directory.

find /etc -name "*conf" -type f -exec grep "XXX.XXX.XXX.XXX" {} \;

Also, provide a uname -a output.

:cool:

Ok, well since no one bothered to answer your first question (although credit to the person who assumed it was a programming question), lemme give it a shot. An IP address in decimal is a representation of a 32 digit binary number, you can convert each section to binary and keeping it in order you will have the binary IP address, for example, let's convert 127.0.0.1, now remembering each octet is 8 digits in a 32 digit binary number, we convert 127 which is 11111111, 0 which is 00000000, and 1 which is 00000001, so 127.0.0.1 is 11111111 0000000 00000000 00000001 where each block represents an IP octet however an IP address 127.0.0.1 is the whole number of 11111111000000000000000000000001.

Just remember, you want to convert each number into an 8 digit binary number, however, the first octet can be shorter if the octet produces a binary number with leading 0's for example, 10.0.0.1 would produce 00001010000000000000000000000001 however that can simply be written as 101000000000000000000000001 since the leading 0's server no purpose and is identical to comparing decimal $500 and $000,500, or 10.5 and 10.50000, they are all the same numbers but leading 0's or trailing decimal 0's just don't change the output.

Now if you want your subnet on linux:
CIDR notation: ip addr ls | grep 'inet ' | cut -d' ' -f6
dotted octet notation: ifconfig | tr ' ' '\n' | grep Mask | cut -d':' -f2

After rereading, you want your subnet, well you need to learn the math more on how to find a subnet from an ip address and a subnet mask.

There are various ways to convert a 32-bit number to a sequence of bits. The "canonical" ordering is called network byte order; there is also the native order for big-endian and little-endian systems. So 1.2.4.8 can be represented as the bytes 1, 2, 4, 8 or 8, 4, 2, 1 or 2, 1, 8, 4 or 4, 8, 1, 2, depending on the word size and endianness of the representation. (There are obviously additional permutations in theory.)

The subnet is traditionally determined from the class of the network. Read up on class A, class B, class C, class D, and the extension called CIDR which allows you to define a netmask which is different from that defined by the class.

Just to clarify, a network class is the traditional and depricated way, see IETF RFC 4632. Classes are only for people that stopped learning networking eons ago. CIDR is how a subnet IS defined by standards and is not an extension.