Created
July 29, 2019 22:51
-
-
Save parzibyte/43ccb3e7dbf28eccaa8a2209e6770259 to your computer and use it in GitHub Desktop.
Entero a partir de hexadecimal - https://parzibyte.me/blog/2018/11/19/entero-a-partir-de-digito-hexadecimal-c/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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