Смотри и изумляйся как это делают фантазеры-энтузиасты
| Код | #include "stdio.h" #include "string.h"
#include "conio.h"
int HexstrToInt( char *hexstr, int control ) { int value=-1, x=0, sixspace=0, n=0;
if( strlen(hexstr) <3 ) { if (hexstr[0] =='a' || hexstr[0] =='A') x =10; else if (hexstr[0] =='b' || hexstr[0] =='B') x =11; else if (hexstr[0] =='c' || hexstr[0] =='C') x =12; else if (hexstr[0] =='d' || hexstr[0] =='D') x =13; else if (hexstr[0] =='e' || hexstr[0] =='E') x =14; else if (hexstr[0] =='f' || hexstr[0] =='F') x =15;
else if (hexstr[0] =='1') x=1; else if (hexstr[0] =='2') x=2; else if (hexstr[0] =='3') x=3; else if (hexstr[0] =='4') x=4; else if (hexstr[0] =='5') x=5; else if (hexstr[0] =='6') x=6; else if (hexstr[0] =='7') x=7; else if (hexstr[0] =='8') x=8; else if (hexstr[0] =='9') x=9; }
else { if (control <2) { //счетчик количества запусков функцей самой себя на 1 char *tmp =new char[2*sizeof(char)]; tmp[0] =hexstr[0]; tmp[1] =hexstr[1]; tmp[2] ='\0'; x =HexstrToInt( tmp, control ); //рекурсия пошла control++; //Контроль глубины delete [] tmp; } else return -1; //какая то ошибка }
if ((n =(strlen(hexstr))) >0) n--;
if (hexstr[n] =='a' || hexstr[n] =='A') sixspace =0; else if (hexstr[n] =='b' || hexstr[n] =='B') sixspace =1; else if (hexstr[n] =='c' || hexstr[n] =='C') sixspace =2; else if (hexstr[n] =='d' || hexstr[n] =='D') sixspace =3; else if (hexstr[n] =='e' || hexstr[n] =='E') sixspace =4; else if (hexstr[n] =='f' || hexstr[n] =='F') sixspace =5;
else if (hexstr[n] =='0') sixspace =-10; else if (hexstr[n] =='1') sixspace =-9; else if (hexstr[n] =='2') sixspace =-8; else if (hexstr[n] =='3') sixspace =-7; else if (hexstr[n] =='4') sixspace =-6; else if (hexstr[n] =='5') sixspace =-5; else if (hexstr[n] =='6') sixspace =-4; else if (hexstr[n] =='7') sixspace =-3; else if (hexstr[n] =='8') sixspace =-2; else if (hexstr[n] =='9') sixspace =-1;
value =(sixspace +(6*x)) +((x+1)*10);
return value; }
void main( int argc, char *argv[] ) { int i=-1; char s[2]; /* ввести надо число типа FF или 1A или 0E именно так без приставок типа 0xAF */ printf( "Enter hex value " ); scanf( "%s", &s); //вводим два символа if( (i=(HexstrToInt( s, 0 ))) >-1 ); printf( "%s -in HEX = %d - in DEC\n", s, i ); getch(); return; }
|
|