package com.roshka.webprojecttest.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/show-me-the-bits")
public class ShowMeTheBitsServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
PrintWriter pw = new PrintWriter(resp.getOutputStream());
Boolean valido=true;
String parametro = req.getParameter("number");
Integer [] vectorResultado = new Integer [16];
if(parametro == null) {
initForm(pw);
}
else {
String tipo="";
if(parametro.length() < 1)
valido=false;
else if(parametro.length() >1 ) {
if(parametro.charAt(0) == '0') {
if(parametro.charAt(1) == 'x')
tipo = "hexadecimal";
else
tipo = "octal";;
}else
tipo="decimal";
}else
tipo= "decimal";;
if(tipo.equals("decimal")) {
for ( int i = 0 ; i< parametro.length(); i++)
if(!Character.isDigit(parametro.charAt(i)))
valido = false;
}
else if(tipo.equals("octal")) {
for ( int i = 1 ; i< parametro.length(); i++)
if(!Character.isDigit(parametro.charAt(i)))
valido = false;
}
else if(tipo.equals("hexadecimal")) {
for ( int i = 2 ; i < parametro.length(); i++ )
if( !isHexaDigit(parametro.charAt(i) ) )
valido = false;
}
int validAux = 0;
if(valido) {
if(tipo.equals("decimal") )
validAux = Integer.parseInt(parametro,10);
else if( tipo.equals("hexadecimal") )
validAux = Integer.parseInt(parametro.substring(2),16);
else
validAux = Integer.parseInt(parametro.substring(1),8);
}
if(validAux > 65535)
valido = false;
if (!valido) {
invalido(pw, parametro);
}
else {
transformacion(tipo, parametro, vectorResultado);
theTable(pw, vectorResultado);
}
}
pw.close();
}
void initForm(PrintWriter pw) {
pw.write(""
+ "
"
+ "Hola, bienvenidos al show me the bits"
+ ""
+ ""
+ "Soy un conversor sistemas de numeracion
"
+ ""
+ ""
+ "
"
+ ""
+ "");
}
void theTable(PrintWriter pw, Integer [] vectorResultado) {
pw.write(""
+ ""
+ "Hola, bienvenidos al conversor de numeros"
+ ""
+ ""
+ "Soy un conversor de sistemas de numeracion
"
+ ""
+ ""
+ "
"
+ ""
+ "");
for(int i = 15; i >= 0 ; i--) {
if(vectorResultado[i] == 1) {
pw.write("" + vectorResultado[i]+" | ");
}
else {
pw.write(""+vectorResultado[i]+" | ");
}
if(i == 8) {
pw.write(" === | ");
}
else if(i == 0) {
pw.write("
");
}
else
pw.write(" | | ");
}
pw.write( "
"
+ ""
+ "");
}
void invalido(PrintWriter pw, String parametro) {
pw.write(""
+ ""
+ "Error de parametro"
+ ""
+ ""
+ "No se puede generarla tabla de bits. Valor del parametro \"" + parametro + "\" invalido."
+ ""
+ "");
}
public void transformacion(String tipo, String valor, Integer [] resultado) {
if(tipo.equals("decimal")) {
Integer numero = Integer.parseInt(valor);
int [] vectorAux = new int[16];
vectorAux[0] = 1;
for(int i = 1 ; i < 16 ;i++) {
vectorAux[i] = vectorAux[i-1]*2;
}
for(int i = 15 ; i > -1 ; i--) {
if(numero >= vectorAux[i]) {
resultado[i]=1;
numero%=vectorAux[i];
}
else {
resultado[i] = 0;
}
}
}
else if(tipo.equals("hexadecimal") ) {
String cadenaAux = "";
int k = 0;
for (int i = valor.length()-1 ; i > 1 ; i--){
cadenaAux = hexaToBin(valor.charAt(i));
for(int j = 3 ; j > -1 ; j--) {
resultado[k++]=Integer.parseInt(""+cadenaAux.charAt(j));
}
}
while(k<16)
resultado[k++]=0;
}
else if( tipo.equals("octal") ) {
String cadenaAux = "";
int k = 0;
for (int i = valor.length()-1 ; i > 0 ; i--){
cadenaAux = octalToBin(valor.charAt(i));
for(int j = 2 ; j > -1; j--) {
resultado[k++]=Integer.parseInt(""+cadenaAux.charAt(j));
}
}
while(k<16)
resultado[k++]=0;
}
}
public String hexaToBin(char caracter) {
if(caracter == 'F')
return "1111";
if(caracter == 'E')
return "1110";
if(caracter == 'D')
return "1101";
if(caracter == 'C')
return "1100";
if(caracter == 'B')
return "1011";
if(caracter == 'A')
return "1010";
if(caracter == '9')
return "1001";
if(caracter == '8')
return "1000";
if(caracter == '7')
return "0111";
if(caracter == '6')
return "0110";
if(caracter == '5')
return "0101";
if(caracter == '4')
return "0100";
if(caracter == '3')
return "0011";
if(caracter == '2')
return "0010";
if(caracter == '1')
return "0001";
if(caracter == '0')
return "0000";
return "";
}
public String octalToBin(char caracter) {
if(caracter == '7')
return "111";
if(caracter == '6')
return "110";
if(caracter == '5')
return "101";
if(caracter == '4')
return "100";
if(caracter == '3')
return "011";
if(caracter == '2')
return "010";
if(caracter == '1')
return "001";
if(caracter == '0')
return "000";
return "";
}
public Boolean isHexaDigit(char caracter) {
if( caracter == '0' ||caracter == '1' ||caracter == '2' ||caracter == '3' ||
caracter == '4' ||caracter == '5' ||caracter == '6' ||caracter == '7' ||
caracter == '8' ||caracter == '9' ||caracter == 'A' ||caracter == 'B' ||
caracter == 'C' ||caracter == 'D' ||caracter == 'E' ||caracter == 'F' ) {
return true;
}
else
return false;
}
}