Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created September 2, 2020 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/6eac3c19b7a1995f3ed966d7623e49ee to your computer and use it in GitHub Desktop.
Save parzibyte/6eac3c19b7a1995f3ed966d7623e49ee to your computer and use it in GitHub Desktop.
// Validadores
public static boolean validarDecimal(int decimal) {
// Decimal pasa la validación con el hecho de que sea entero
return true;
}
public static boolean validarBinario(int binario) {
// Comprobar si solo se compone de unos y ceros
String binarioComoCadena = String.valueOf(binario);
for (int i = 0; i < binarioComoCadena.length(); i++) {
char caracter = binarioComoCadena.charAt(i);
if (caracter != '0' && caracter != '1') {
return false;
}
}
return true;
}
public static boolean validarOctal(int octal) {
// comprobar si solo tiene números del 0 al 7
String octalComoCadena = String.valueOf(octal);
String caracteresOctales = "01234567";
for (int i = 0; i < octalComoCadena.length(); i++) {
char caracter = octalComoCadena.charAt(i);
// Si no se encuentra dentro de los caracteres válidos, regresamos false
if (caracteresOctales.indexOf(caracter) == -1) {
return false;
}
}
return true;
}
// Nota: se debe enviar la cadena hexadecimal convertida a mayúsculas
public static boolean validarHexadecimal(String hexadecimal) {
// Comprobar si solo tiene números del 0 al 9 y letras de la A a la F
String caracteresHexadecimales = "0123456789ABCDEF";
for (int i = 0; i < hexadecimal.length(); i++) {
char caracter = hexadecimal.charAt(i);
// Si no se encuentra dentro de los caracteres válidos, regresamos false
if (caracteresHexadecimales.indexOf(caracter) == -1) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment