void bin2Dec(int binaryNumber, int * decimal,int* weight){ int bit; if(binaryNumber>0){ bit = binaryNumber %10; *decimal += bit * (int)(pow(2.0,(double)*weight)); binaryNumber = binaryNumber /10; *weight+=1; bin2Dec(binaryNumber,decimal,weight); } }
int main(){ int decimalNum,bitWeight,binaryNum; decimalNum = 0; bitWeight = 0; printf("Please enter number in binary: "); scanf("%d",&binaryNum); bin2Dec(binaryNum, &decimalNum, &bitWeight); printf("The result is :%d",binaryNum); return 0; }
void app() { //UP TO RESET if (input & KEY_UP) variableReset(); //CHECK PRESSED NUMBERS //Get input //DECIMAL INPUT if ((posX >= 10 && posX <= 51) && (posY >= 9 && posY <= 48))PRESSED_NUMBER = 1; else if ((posX >= 55 && posX <= 95) && (posY >= 9 && posY <= 48))PRESSED_NUMBER = 2; else if ((posX >= 100 && posX <= 140) && (posY >= 9 && posY <= 48))PRESSED_NUMBER = 3; else if ((posX >= 10 && posX <= 51) && (posY >= 53 && posY <= 95))PRESSED_NUMBER = 4; else if ((posX >= 55 && posX <= 95) && (posY >= 53 && posY <= 95))PRESSED_NUMBER = 5; else if ((posX >= 100 && posX <= 140) && (posY >= 53 && posY <= 95))PRESSED_NUMBER = 6; else if ((posX >= 10 && posX <= 51) && (posY >= 100 && posY <= 140))PRESSED_NUMBER = 7; else if ((posX >= 55 && posX <= 95) && (posY >= 100 && posY <= 140))PRESSED_NUMBER = 8; else if ((posX >= 100 && posX <= 140) && (posY >= 100 && posY <= 140))PRESSED_NUMBER = 9; else if ((posX >= 10 && posX <= 77) && (posY >= 145 && posY <= 193))PRESSED_NUMBER = 0; else if ((posX >= 82 && posX <= 140) && (posY >= 145 && posY <= 193)) PRESSED_NUMBER = -1; //Decimal "Cancel" button//BINARY KEYBOARD else if ((posX >= 124 && posX <= 184) && (posY >= 202 && posY <= 236))PRESSED_NUMBER = 20; //Binary "0" else if ((posX >= 59 && posX <= 118) && (posY >= 202 && posY <= 236))PRESSED_NUMBER = 21; //Binary "1" else if ((posX >= 189 && posX <= 250) && (posY >= 202 && posY <= 236)) PRESSED_NUMBER = -2; //Binary "Cancel" button//HEX KEYBOARD else if ((posX >= 179 && posX <= 209) && (posY >= 9 && posY <= 48))PRESSED_NUMBER = 31; //hex 1 else if ((posX >= 213 && posX <= 244) && (posY >= 9 && posY <= 48))PRESSED_NUMBER = 32; //hex 2 else if ((posX >= 247 && posX <= 279) && (posY >= 9 && posY <= 48))PRESSED_NUMBER = 33; //hex 3 else if ((posX >= 179 && posX <= 209) && (posY >= 54 && posY <= 96))PRESSED_NUMBER = 34; //hex 4 else if ((posX >= 213 && posX <= 244) && (posY >= 54 && posY <= 96))PRESSED_NUMBER = 35; //hex 5 else if ((posX >= 247 && posX <= 279) && (posY >= 54 && posY <= 96))PRESSED_NUMBER = 36; //hex 6 else if ((posX >= 179 && posX <= 209) && (posY >= 108 && posY <= 140))PRESSED_NUMBER = 37; //hex 7 else if ((posX >= 213 && posX <= 244) && (posY >= 108 && posY <= 140))PRESSED_NUMBER = 38; //hex 8 else if ((posX >= 247 && posX <= 279) && (posY >= 108 && posY <= 140))PRESSED_NUMBER = 39; //hex 9 else if ((posX >= 179 && posX <= 226) && (posY >= 146 && posY <= 176))PRESSED_NUMBER = 30; //hex 0 else if ((posX >= 231 && posX <= 279) && (posY >= 146 && posY <= 176))PRESSED_NUMBER = -3; //hex "Cancel" else if ((posX >= 283 && posX <= 313) && (posY >= 9 && posY <= 31))PRESSED_NUMBER = 41; //hex A else if ((posX >= 283 && posX <= 313) && (posY >= 36 && posY <= 59))PRESSED_NUMBER = 42; //hex B else if ((posX >= 283 && posX <= 313) && (posY >= 64 && posY <= 89))PRESSED_NUMBER = 43; //hex C else if ((posX >= 283 && posX <= 313) && (posY >= 94 && posY <= 118))PRESSED_NUMBER = 44; //hex D else if ((posX >= 283 && posX <= 313) && (posY >= 122 && posY <= 148))PRESSED_NUMBER = 45; //hex E else if ((posX >= 283 && posX <= 313) && (posY >= 153 && posY <= 176))PRESSED_NUMBER = 46; //hex F else PRESSED_NUMBER = 10; //Process input and conversion if (OLD_PRESSED_NUMBER == 10 && PRESSED_NUMBER != 10) { if (PRESSED_NUMBER == 20 || PRESSED_NUMBER == 21 || PRESSED_NUMBER == -2) { //BINARY--->DECIMAL & HEX: if (PRESSED_NUMBER != -2) { if (bit_count < 20){ BINARY *= 10; BINARY += (PRESSED_NUMBER - 20); bit_count++; } } else { BINARY /= 10; bit_count--; } DECIMAL = bin2Dec(BINARY); dec2Hex(DECIMAL, HEX); } else if (posX < 160) { //DECIMAL--->BINARY & HEX: if (PRESSED_NUMBER != -1) { if (DECIMAL*10+PRESSED_NUMBER<=maxDec){ DECIMAL *= 10; DECIMAL += PRESSED_NUMBER; } } else DECIMAL /= 10; BINARY = dec2Bin(DECIMAL); dec2Hex(DECIMAL, HEX); } else { //HEX--->DECIMAL&BIN if (PRESSED_NUMBER == -3) { if (hex_count != -1) { HEX[hex_count] = '\0'; hex_count--; } } else if (hex_count!=4) { hex_count++; if (PRESSED_NUMBER > 40 && PRESSED_NUMBER < 48) { if (PRESSED_NUMBER == 41)HEX[hex_count] = 'A'; if (PRESSED_NUMBER == 42)HEX[hex_count] = 'B'; if (PRESSED_NUMBER == 43)HEX[hex_count] = 'C'; if (PRESSED_NUMBER == 44)HEX[hex_count] = 'D'; if (PRESSED_NUMBER == 45)HEX[hex_count] = 'E'; if (PRESSED_NUMBER == 46)HEX[hex_count] = 'F'; } else { if(!(PRESSED_NUMBER==30 && hex_count==0)) HEX[hex_count] = PRESSED_NUMBER - 30 + 48; else hex_count--; } } if (hex2Dec(HEX) > maxDec) { HEX[hex_count] = '\0'; hex_count--; } DECIMAL = hex2Dec(HEX); BINARY = dec2Bin(DECIMAL); } } OLD_PRESSED_NUMBER = PRESSED_NUMBER; }