hello what error does this code have to call hexValid twice and read 4 hexadecimal characters [closed]

0
/*
**Fichero: hexDec3.c
** Autor: Francisco Javier Peña Higueras
** Fecha: 5-11-2018
**
**Descripcion:Realiza el cambio de dos dígitos hexadecimales a decimales
**
*/
#include <stdio.h>
#define CTE 16
#define SINERROR 0
#define CONERROR 1
#define REPETIR 0
#define SALIR 1
#define VALIDO 1
#define INVALIDO 0

int hexValidos (char caar1, char caar2);
int  hexDec (char caar1, char caar2);

int main(){
char car1;
char car2;
 char car3;
 char car4;
 int valor;
int valor1;
 int valor2;
 int valor3;
 int valor4;
int salir=REPETIR;
 int validador;
 do{
   printf("Introduzca dos digitos hexadecimales: ");
   scanf("\n%c%c",&car1,&car2, &car3, &car4);

   if(car1=='N' && car2=='A'){
     salir=SALIR;
   }
   else{

    validador=hexValidos(car1, car2);
    if(validador == VALIDO){

     valor1 =hexDec(car1, car2);
     valor2 =hexDec(car1, car2);
    }

    validador=hexValidos(car3, car4);
    if(validador == VALIDO){

       valor3 = hexDec(car3, car4);
     valor4 = hexDec(car3, car4);
      }





    valor =hexDec(car1,car2);
      if((validador == VALIDO) && (validador == VALIDO) && (validador == VALIDO) && (validador == VALIDO))
    printf("Valor Hexadecimal: 0x%c%c, Valor Decimal: %d\n",car1, car2,car3, car4,  valor);
 }while (salir!=SALIR);
 return 0;
}
int hexDec (char caar1, char caar2)
{
  int valor;
  int valor1;
  int valor2;

 if ((caar1 >= '0') && (caar1 <='9'))
   {
 valor1 = caar1 - '0';
   }
     else
       {
     if
       ((caar1 >= 'a') && (caar1 <= 'f'))
       {
     valor1 = caar1 - 'a' + 10;
       }

        else
          {
        valor1 =caar1 - 'A' + 10;
          }
       }
        if ((caar2 >= '0') && (caar2 <= '9'))
          {
          valor2 = caar2 - '0';
          }
        else
          {
            if((caar2 >= 'a') && (caar2 <= 'f'))
              {
               valor2 = caar2 - 'a' + 10;
          }
               else
             {
               valor2 = caar2 - 'A' + 10;
             }
          }




 valor = CTE * valor1 + valor2; 


  return  valor;
}

int hexValidos(char caar1, char caar2){

  int cuno;
  int cdos;
  int validador;

  if( (caar1 >= '0' && caar1 <='9') || (caar1 >= 'A' && caar1 <= 'F') || (caar1 >= 'a' && caar1 <= 'f'))
   {
                     cuno = SINERROR;
                     validador = VALIDO;
   }
 else
   {
     cuno = CONERROR;
     validador = INVALIDO;

   }
     if (cuno == CONERROR)
       {
       printf("Primer valor introducido erroneo \n");

       }


        if ((caar2 >= '0' && caar2 <='9') || (caar2 >= 'A' && caar2 <= 'F') || (caar2 >= 'a' && caar2 <= 'f'))
          {
          cdos = SINERROR;
          validador = VALIDO;
          }
        else
          {
            cdos = CONERROR;
            validador = INVALIDO;
          }
            if (cdos == CONERROR)
              {
              printf ("Segundo valor introducido erroneo \n");

              }

            return validador;

}
    
asked by Ronald Mijares 18.11.2018 в 09:54
source

1 answer

2

I do not understand what you mean by the question.

However, there is a macro in <ctype.h> called isxdigit that verifies if a character is a valid hexadecimal digit. This can help you reduce or eliminate the hexValidos function.

In addition, there are the toupper and tolower functions that convert a character to uppercase and lowercase respectively. This can be useful to reduce or eliminate comparisons with uppercase and lowercase in hexDec .

In any case I suggest you see the function strtol , which can do exactly the conversion that you need.

    
answered by 19.11.2018 в 06:23