* Class that Support my convertion between binary to number
* @Author : KURNIAWAN Kurniawan
* @STUDENT ID : 2791692
* @Created Date : 13/03/2006
* @Modified Date: 16/03/2006
*
*/
import java.lang.Math.*;
import java.util.*;
public class BinHelp
{
public static int bin2int(int[] biner,int start,int size)
{
int sum=0;
int ctr=start;
for (int i=0;i
sum+=biner[ctr] * (Math.pow(2,(size-1-i)));
ctr++;
}
return sum;
}
public static int[] int2bin(int dec, int bits)
{
int[] biner= new int[bits];
for (int i=0;i
biner[i]= 0;
}
int i=0;
if (dec>0)
{
while(dec!=1)
{
biner[bits-1-i]= dec % 2;
dec=(int)dec/2;
i++;
}
//the last one
biner[bits-1-i]= dec;
}
return biner;
}
//this function is to convert binary to byte size
public static byte bin2byte(int[] biner,int start,int size)
{
return int2byte(bin2int(biner,start,size));
}
public static int[] byte2bin(byte dec,int size)
{
return int2bin(byte2int(dec),size);
}
public static int [] join(int [] a, int [] b)
{
int [] ints = new int[a.length + b.length];
System.arraycopy(a, 0, ints, 0, a.length);
System.arraycopy(b, 0, ints, a.length, b.length);
return ints;
}
public static int [] extract(int [] a,int start, int size)
{
int [] result= new int[size];
int ctr=start;
for (int i=0;i
result[i]=a[ctr];
ctr++;
}
return result;
}
public static int[] createIP(int octet1,int octet2,int octet3,int octet4)
{
int[][] ip= new int [4][8];
ip[0]=int2bin(octet1,8);
ip[1]=int2bin(octet2,8);
ip[2]=int2bin(octet3,8);
ip[3]=int2bin(octet4,8);
int[] result;
result= join(ip[0],ip[1]);
result= join(result,ip[2]);
result= join(result,ip[3]);
//printBin(result,32);
return result;
}
public static void printBin(int[] biner,int size)
{
for (int i=0;i
System.out.print(biner[i] + " ");
}
System.out.println();
}
public static void displayIP(int[] ip)
{
int[][] ipx= new int [4][8];;
for (int i=0;i<4;i++)
{
ipx[i]=extract(ip,i*8,8);
System.out.print (bin2int(ipx[i],0,8) + " ");
}
System.out.println();
}
public static int byte2int(byte x){return (int)x+128;}//int start 0-255
public static byte int2byte(int x){return (byte)(x-128);}//byte -128 to 128
public static void main(String[] args)
{
System.out.println((byte)129);
}
}
No comments:
Post a Comment