Skip to content
Snippets Groups Projects
Commit e731c9c4 authored by Strozzi's avatar Strozzi
Browse files

Change the hex2int function

parent 7672348b
Branches
No related tags found
No related merge requests found
Pipeline #
...@@ -23,63 +23,12 @@ int hex2int(char *hex) ...@@ -23,63 +23,12 @@ int hex2int(char *hex)
{ {
int s= Strlen(hex), i, ans= 0; int s= Strlen(hex), i, ans= 0;
for (i= 0; i< s; i++) { for (i= 0; i< s; i++) {
switch(hex[i]) { hex[i]-= 48;
case 'F': if (hex[i] > 9)
case 'f': hex[i] -= 7;
ans+= 15*pow16(s-(i+1)); if (hex[i] > 15)
break; hex[i] -= 32;
case 'E': ans += hex[i] * pow16(s-(i+1));
case 'e':
ans+= 14*pow16(s-(i+1));
break;
case 'D':
case 'd':
ans+= 13*pow16(s-(i+1));
break;
case 'C':
case 'c':
ans+= 12*pow16(s-(i+1));
break;
case 'B':
case 'b':
ans+= 11*pow16(s-(i+1));
break;
case 'A':
case 'a':
ans+= 10*pow16(s-(i+1));;
break;
case '9':
ans+= 9*pow16(s-(i+1));;
break;
case '8':
ans+= 8*pow16(s-(i+1));;
break;
case '7':
ans+= 7*pow16(s-(i+1));;
break;
case '6':
ans+= 6*pow16(s-(i+1));;
break;
case '5':
ans+= 5*pow16(s-(i+1));;
break;
case '4':
ans+= 4*pow16(s-(i+1));;
break;
case '3':
ans+= 3*pow16(s-(i+1));;
break;
case '2':
ans+= 2*pow16(s-(i+1));;
break;
case '1':
ans+= 1*pow16(s-(i+1));;
break;
default:
if (hex[i] != '0')
printf("ERROR!\n");
break;
}
} }
return ans; return ans;
} }
...@@ -110,12 +59,12 @@ int main() ...@@ -110,12 +59,12 @@ int main()
{ {
char n[16], f[16]; char n[16], f[16];
int fib; int fib;
while (scanf("%s", n) > 0) { while (scanf("%s", n) && n[0]) { // trocar scanf por getc()
fib= hex2int(n); fib= hex2int(n);
if (fib < BUF_SZ) { if (fib < BUF_SZ) {
fib = buf[fib]; fib = buf[fib];
int2hex(f, fib); int2hex(f, fib);
printf("%d(%s)\n", fib, f); printf("%d(%s)\n", fib, f); // trocar printf por putc()
} else } else
printf("Valor fora dos limites!\n"); printf("Valor fora dos limites!\n");
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment