Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created July 29, 2019 22:51
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/43ccb3e7dbf28eccaa8a2209e6770259 to your computer and use it in GitHub Desktop.
Save parzibyte/43ccb3e7dbf28eccaa8a2209e6770259 to your computer and use it in GitHub Desktop.
/*
Obtener representación entera de carácter hexadecimal
@author parzibyte
Visita: parzibyte.me
*/
#include <stdio.h> // Para printf
#include <ctype.h> // Para toupper y isdigit
// Prototipo de función
int caracterHexadecimalADecimal(char caracter);
int main() {
// Para probarlos a todos
char *digitos = "0123456789ABCDEF";
for (int x = 0; digitos[x] != 0; x++) {
char digito = digitos[x];
int equivalencia = caracterHexadecimalADecimal(digito);
printf("El carácter hexadecimal '%c' es %d en decimal\n", digito,
equivalencia);
}
return 0;
}
int caracterHexadecimalADecimal(char caracter) {
if (isdigit(caracter))
return caracter - '0';
return 10 + (toupper(caracter) - 'A');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment