Visit Our UNIX and Linux User Community


converting ip addresses to binary


 
Thread Tools Search this Thread
Special Forums IP Networking converting ip addresses to binary
# 1  
Old 03-26-2002
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???
# 2  
Old 03-27-2002
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.
# 3  
Old 03-29-2002
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.

Smilie
# 4  
Old 10-02-2008
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
# 5  
Old 10-02-2008
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.
# 6  
Old 10-02-2008
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.
# 7  
Old 10-02-2008
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #786
Difficulty: Medium
In 2019, Bill Gates had a net worth estimated to be over $200 billion USD.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Converting binary file to text file

Hi, Im wondering how I can convert a binary file to a text file? I have ran the following command to output which type of binary file coding it is od -t x1 -c eHat.data0 | head -20 and that gives me the following output; 0000000 5c 00 00 00 cd 06 f2 41 00 00 00 c6 00 00 00 c6 \ \0 \0 \0... (3 Replies)
Discussion started by: dp0b
3 Replies

2. Shell Programming and Scripting

Converting a binary file to ascii and vice versa?

Hi All, I have a binary file which is being exported from a Database, and i need to convert that to ASCII format. How can i achieve that? And this solution should work for any file which is given to us; means they will give different files from different tables. Thanks in advance. (8 Replies)
Discussion started by: baranisachin
8 Replies

3. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

4. UNIX for Dummies Questions & Answers

Converting binary file to readable format in Ksh

In Unix/Ksh, when I try to look inside a file it says that the file may be a binary file and if I want to see it anyway. When i say 'yes', it shows me the content filled with unreadable symbols (looks like binary). Is there a command that I can run from the Unix prompt to convert/translate that... (3 Replies)
Discussion started by: arthurs
3 Replies

5. Programming

Converting HexDecimal To Binary - 'C' programming

guys, Can somebody help me with a Unix 'C' program, to convert the Hexa decimal value to binary value. Thank you Cheers :) S (3 Replies)
Discussion started by: sudharma
3 Replies

6. Linux

Converting source rpm to binary rpm

Hi, I need to install an rpm file,but I only have '.src.rpm' version of it. I want to convert it to a binary rpm. so I tried this rpm --rebuild somethin.src.rpm But I am gettin '--rebuild:Invalid option' as output Is there another way to rebuild source rpm.? Thanks in... (2 Replies)
Discussion started by: eamani_sun
2 Replies

7. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies

8. UNIX for Advanced & Expert Users

Converting ASCII to Binary mode

Dear All, Business Users are transfering ( FTP ) a CSV file into the IBM AIX box with transfer mode as ASCII. But I want to convert the CSV file from ASCII mode into binary mode, as my script expects file in binary mode. Is it possible to do through Unix commands? Thanks in Advance, RK (1 Reply)
Discussion started by: srajeshmca
1 Replies

9. Shell Programming and Scripting

unix script for converting a decimal to binary

Could anybody please help me in writing a script in unix for converting a decimal number to binary number. (3 Replies)
Discussion started by: softy
3 Replies

10. IP Networking

ip addresses

this might sound corny but i need help finding out about finding a persons ip address can u halp? and another question what the hell does ping mean??? thank u. (2 Replies)
Discussion started by: dragonslayer100
2 Replies

Featured Tech Videos