Asudahlah.com

Programming, tutorials, mechatronics, operating systems, and other tech stuff

How to make ip address calculator with java netbeans

6 comments

This one is a video tutorial. And here is the source code which is used in the tutorial.
For step-by-step instruction, please watch the video.


int prefix = Integer.valueOf(mask.getText());
int pos = prefix / 8; //important octet position
int rem = prefix % 8; //binary digit of importan octet

String binIpFull = "";
String subBinary = "";
String subBinaryFull = "";
String binnetidFull = "";
String binbcidFull = "";
String netmaskFull = "";
String netidFull = "";
String bcidFull = "";

//loop to generate binary mask from octet pos
for (int i = 1; i & lt; = 8; i++) {
 if (i & lt; = rem) {
  subBinary += "1";
 } else {
  subBinary += "0";
 }
}

//netmask (integer version of subBinary
int netmask = Integer.parseInt(String.valueOf(subBinary), 2);

//ip octets array
int[] oct = {
 Integer.valueOf(oct1.getText()),
 Integer.valueOf(oct2.getText()),
 Integer.valueOf(oct3.getText()),
 Integer.valueOf(oct4.getText())
};

//binary of important octet
String binOct = String.format("%8s", Integer.toBinaryString(oct[pos])).replace(' ', '0');

String binNetId = "";
String binBcId = "";

for (int i = 0; i & lt; 8; i++) {
 String binPointer = subBinary.substring(i, i + 1);
 if (binPointer.equals("1")) {
  binNetId += binOct.substring(i, i + 1);
  binBcId += binOct.substring(i, i + 1);
 } else {
  binNetId += "0";
  binBcId += "1";
 }
}

//loop to generate display-ready strings
for (int i = 0; i & lt; = 3; i++) {
 binIpFull += String.format("%8s", Integer.toBinaryString(oct[i])).replace(' ', '0');
 if (i < pos) { = ""
  subbinaryfull += "11111111"; = ""
  netmaskfull = "" += "255"
  binnetidfull = ""
  ','
  0 ');="" binbcidfull="" netidfull="" bcidfull="" ="" }else="" if(i="">pos){
  subBinaryFull += "00000000";
  netmaskFull += "0";
  binnetidFull += "00000000";
  binbcidFull += "00000000";
  netidFull += "0";
  bcidFull += "1";
 } else {
  subBinaryFull += subBinary;
  netmaskFull += String.valueOf(netmask);
  binnetidFull += binNetId;
  binbcidFull += binBcId;
  netidFull += Integer.parseInt(String.valueOf(binNetId), 2);
  bcidFull += Integer.parseInt(String.valueOf(binBcId), 2);
 }
 if (i != 3) {
  subBinaryFull += ".";
  netmaskFull += ".";
  binnetidFull += ".";
  binbcidFull += ".";
  netidFull += ".";
  bcidFull += ".";
  binIpFull += ".";
 }
}
subBinLabel.setText(subBinaryFull);
netMaskLabel.setText(netmaskFull);
netBinLabel.setText(binnetidFull);
bcBinLabel.setText(binbcidFull);
netIdLabel.setText(netidFull);
bcIdLabel.setText(bcidFull);
ipBinLabel.setText(binIpFull);


You can watch the video here:



6 comments :

Post a Comment

[PHP FPDF] How to Integrate QR Code in PDF Generation

22 comments
After long time without update, this time, i will post about how to use QR Code in PDF created with PHP FPDF Library.


For those who doesn't know what QR Code is, here's a brief summary. QR Code, invented in 1994 by the Japanese company Denso Wave, is a 2 dimensional version of barcode. And unlike barcode, QR Code are capable to store alphanumeric, symbol, binaries, and kanji. Right now, it's widely used in many application such as product identification, payment, data entries, advertisement, announcement, and many more. In short, it looks like this:
And you can scan it with your smartphone to get information contained in it.

Now, let's get back to the topic.

In my previous tutorial about How to add barcode to PDF created with PHP FPDF, we extend our FPDF class so it could provide barcode generation suport. But this time, we are using an entirely independent library called PHP QR Code by deltalab.
Download the PHP QR Code library here : http://sourceforge.net/projects/phpqrcode/files/

Then extract it to your working directory alongside the fpdf directory.

Then from the browser, try to open that directory to try the demos. If nothing goes wrong, you will be presented by something like this.


This is a general-purpose QR Code generator, which means you can use it in another project other than PDF.

Now, create a new php file in your working directory. Then make a blank pdf page out of it.
Please read Generate Printable Invoice in PHP using FPDF library if you haven't done so.

Add this code in your new file:
<?php
require_once("fpdf17/fpdf.php");

$pdf = new FPDF('P','mm','A4');

$pdf->AddPage();

$pdf->Output();
And the result should be a blank pdf page like this


Before we continue, there are 2 method to insert the QR Code into our PDF File. And both works by using FPDF Image method to insert an image in the pdf.

First method is by using a QR Code generator, second is generating a QR Code image file upon PDF Generation.

Let's try out the former.

Create a QR Code Generator.

Create a new php file, name it qr_generator.php. Then add these 2 simple lines of code in it.
<?php
require_once("phpqrcode/qrlib.php");

QRcode::png($_GET['code']);
Then try to access that file in browser and provide value to code parameter like this:
http://localhost/pdf/qr_generator.php?code=1234567
and you should get something like this
Congratulation, you've just made a general-purpose QR code generator. Now let's embed it into our PDF Generator.
Simply just add an image using FPDF Image method with that url as the image source parameter like this
<?php
require_once("fpdf17/fpdf.php");

$pdf = new FPDF('P','mm','A4');

$pdf->AddPage();

$pdf->Image("http://localhost/pdf/qr_generator.php?code=content here", 10, 10, 20, 20, "png");

$pdf->Output();
and you should get something like this

Just change the code parameter and image position to suit what you need. With this, you can use the qr code generator for your other project. Just make sure to secure your generator with secret key or tokens, i won't explain it here but to help you start, just use a simple IF statement to check if there are specified secret key passed in the GET request and immediately exit the code if it turns out false.

Now let's try the second method.

Create the QR Code upon PDF Generation.

With this method, instead of using separate QR code generator. We are generating the QR image inside our PDF generator script, store the QR image in a directory, then insert the image using FPDF Image method.

First, include the phpqrcode library in your file.
Then create the QR code using QRcode::png method
QRcode::png("coded number here","test.png");
Note that i added second parameter to the method which indicates that the resulted file will be saved as an png file with that name.

Then add that image in the pdf using Image method like usual. So your final code should look like this (I've provided comments to help explain each parts of the code) :
<?php
//include the libraries
require_once("fpdf17/fpdf.php");
require_once("phpqrcode/qrlib.php");

//create a QR Code and save it as a png image file named test.png
QRcode::png("coded number here","test.png");

//pdf stuff
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();

//this is the first method
$pdf->Image("http://localhost/pdf/qr_generator.php?code=content here", 10, 10, 20, 20, "png");

//this is the second method
$pdf->Image("test.png", 40, 10, 20, 20, "png");

//and absolutely don't forget this one
$pdf->Output();

And if nothing goes wrong, you should get something like this:
IMPORTANT NOTICE : In real world practice, it's necessary to use different file name for each PHP session because the png file would be replaced at every request. And also don't forget to remove the file regularly to prevent it from bloating your storage with a lot of unused png file.

Both method have its pros and cons, just pick whichever method suit your need.

Finally, please check out my youtube video here and please kindly subscribe to my channel for more tutorials. https://www.youtube.com/watch?v=XFpuxgpUXgE



22 comments :

Post a Comment