Skip to content

Instantly share code, notes, and snippets.

@mancusoa74
Created February 15, 2022 11:09
Show Gist options
  • Save mancusoa74/4ac504077c19ed2dda69d523c58267aa to your computer and use it in GitHub Desktop.
Save mancusoa74/4ac504077c19ed2dda69d523c58267aa to your computer and use it in GitHub Desktop.
#include<stdio.h>
#define DIGIT 4
void dec2hex(int n){
int resto = 0, carattere = 0, i = 0;
char hex[DIGIT] = {'0', '0', '0', '0'};
printf("0x");
while(n != 0){
resto = n % 16;
if(resto<10)
carattere = (n % 16) + '0';
else
carattere = (n % 16) + 'A' - 10;
hex[i++]=carattere;
n /= 16;
}
for(int i = DIGIT - 1;i >= 0;i--){
printf("%c", hex[i]);
}
printf("\n");
}
int main(){
int numero;
printf("Inserisci un numero intero tra 0 e 65535:");
scanf("%d", &numero);
printf("%d = ", numero);
dec2hex(numero);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment