In Linux, awk converts IP addresses to decimal to binary and numeric modes.

Source: Internet
Author: User
Tags decimal to binary
This article will introduce you to an example about how to convert IP addresses to binary and numeric MODES & convert decimal to binary in Linux. I hope this example will help you. This article will introduce you to an example about how to convert IP addresses to binary and numeric MODES & convert decimal to binary in Linux. I hope this example will help you.

[Root @ LookBack ~] # Echo 8.8.8.8 | awk-F. '{for (I = 1; I <= NF; I ++) {a = ""; B = $ I; while (B) {a = B % 2; B = int (B/2)} printf ("% 08d % s", a, I! = NF? ".": "\ N ")}}'
20171000.00001000.00001000.00001000
[Root @ LookBack ~] #
[Root @ LookBack ~] #
[Root @ LookBack ~] # Echo 8.8.8.8 | awk-F. '{printf "% d \ n", ($1*(2 ^ 24) + ($2*(2 ^ 16 )) + ($3*(2 ^ 8) + $4 }'
134744072
[Root @ LookBack ~] #
[Root @ LookBack ~] #
[Root @ LookBack ~] # Echo 00001000.00001000.00001000.00001000 | awk-F. 'function bin2dec (a, B, I, c) {B = length (a); c = 0; for (I = 1; I <= B; I ++) {c + = c; if (substr (a, I, 1) = "1") c ++} return c} {for (j = 1; j <= NF; j ++) printf ("% d % s", bin2dec ($ j), j! = NF? ".": "\ N ")}'
8.8.8.8
[Root @ LookBack ~] #
[Root @ LookBack ~] #
[Root @ LookBack ~] # Echo 1000.1000.1000.1000 | awk-F. 'function bin2dec (a, B, I, c) {B = length (a); c = 0; for (I = 1; I <= B; I ++) {c + = c; if (substr (a, I, 1) = "1") c ++} return c} {for (j = 1; j <= NF; j ++) printf ("% d % s", bin2dec ($ j), j! = NF? ".": "\ N ")}'
8.8.8.8
[Root @ LookBack ~] #
# Splitting description
Awk-F. '{# use. As a separator;
For (I = 1; I <= NF; I ++) {# divide the IP address into 4 segments and enter the loop in sequence;
A = ""; # define a variable. The current value of a is null;
B = $ I; # define a variable B value as the value that enters the cycle;
While (B) {#0 in the awk is false, so only B = 0 enters the cyclic body;
A = B % 2 a; # suppose B = 8 here a = 0 "" here "" is the role of a defined above, when B = 4 enters the cyclic body next time, a = 00, and B = 2 enters the cyclic body next time a = 000;
B = int (B/2) # Here is the part of the result integer calculated in the simulated decimal to binary format divided by 2;
}
Printf ("% 08d % s", a, I! = NF? ".": "\ N") # here is actually if (I! = NF) {printf "% 08d", a} else {printf "% 08d \ n", a} Three-way writing method, to print 8 bits of binary less than 0 complement;
}
}'
### The following is the online awk Implementation of decimal to Binary Conversion
# Https://www.cs.ucsb.edu /~ Sherwood/awk/decimal2binary.awk.txt
#! /Usr/bin/gawk-f
#__________________________
#||||||__ |__] | ___
# |__/|__ | ___ | |||||| ___
#
# The scripts were written to be usefull in
# A research enviornment, but anyone is welcome
# To use them. Happy awking.-Tim Sherwood

Func get01string (innum, t, retstr, I ){
Retstr = "";
T = innum;
While (t ){
If (t % 2 = 0 ){
Retstr = "0" retstr;
}
Else {
Retstr = "1" retstr;
}
T = int (t/2 );
}
Return retstr;
}

{
Print get01string ($1 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.