void askIp(int ip[]) { int tableLength = 8; char a[tableLength], b[tableLength], c[tableLength], d[tableLength]; printf("Enter an ip address: "); scanf("%d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]); printf("===================================================================\ \n"); printf("Your IP address in decimal : %d.%d.%d.%d\n", ip[0], ip[1], ip[2]\ , ip[3]); strcpy(a, decToHex(ip[0])); strcpy(b, decToHex(ip[1])); strcpy(c, decToHex(ip[2])); strcpy(d, decToHex(ip[3])); printf("Your IP address in hexadecimal: %s.%s.%s.%s\n", a, b, c, d); strcpy(a, decToOct(ip[0])); strcpy(b, decToOct(ip[1])); strcpy(c, decToOct(ip[2])); strcpy(d, decToOct(ip[3])); printf("Your IP address in octal : %s.%s.%s.%s\n", a, b, c, d); strcpy(a, decToBin(ip[0])); strcpy(b, decToBin(ip[1])); strcpy(c, decToBin(ip[2])); strcpy(d, decToBin(ip[3])); printf("Your IP address in binary : %s.%s.%s.%s\n", a, b, c, d); printf("===================================================================\ \n"); }
void MIDI_Fighter::loadConfig(){ FILE * fd = NULL; fd = fopen(CONFIG_FILE, "r"); if (fd != NULL){ //TODO: Load the file and do things } else{ LOG(ERR, "MIDIFighter", "Error loading MIDIFighter config file. Reverting to hex codes"); //HACK: test string loader Job* tmp = new Job(); tmp->addAction(new Action_string("Hello World!\\VK_RETURN;")); //HACK: end test for (int j = 0; j < NUM_BANKS; j++){ for (int i = 0; i < BTNS_PER_BANK; i++){ if (i == 0){ banks[j].btn[i] = tmp; } else{ banks[j].btn[i] = new Job(); banks[j].btn[i]->addAction(new Action_key(decToHex(i), true)); banks[j].btn[i]->addAction(new Action_key(decToHex(i), false)); } } } return; } }
/* returns the count of messages */ void handleMessageCount() { int size = getSmsCount(); enqueueString(&replyBuffer, "MC", 2); enqueue(&replyBuffer, decToHex(size/16)); enqueue(&replyBuffer, decToHex(size%16)); }
// PURPOSE OF THIS FUNCTION IS TO CONVERT A GIVEN CHAR TO URL HEX FORM std::string CURLEncode::convert(char val) { std::string csRet; csRet += "%"; csRet += decToHex(val, 16); return csRet; }
// Sets rA and rB for printReg function void set_rA_rB() { regsValid = 1; rA_rB = instrBuffer[1]; rA_rB_arr = decToHex(&rA_rB); rA = (nibble) rA_rB_arr[0]; rB = (nibble) rA_rB_arr[1]; }
// PURPOSE OF THIS FUNCTION IS TO CONVERT A GIVEN CHAR TO URL HEX FORM CString CURLEncode::convert(char val) { CString csRet; csRet += "%"; csRet += decToHex(val); return csRet; }
/* delete a message from the device's database */ void handleDeleteMessage() { unsigned int index = hexToDec(dequeue(&requestBuffer))*16 + hexToDec(dequeue(&requestBuffer)); unsigned int result = deleteSmsWithIndex(index); /*if success, refresh screen*/ if(result == 0) { setRefreshDisplay(); refreshDisplay(); } /*enqueue reply*/ enqueueString(&replyBuffer, "DE", 2); enqueue(&replyBuffer, '0'); enqueue(&replyBuffer, decToHex(result)); }
int main(int argc, char *argv[]) { int i, selectIndex, tintValue, optionSwitch, negativeIndex, yiq, terse, enableRed, enableBlue, enableGreen, ret, exitStatus; i = yiq = terse = tintValue = optionSwitch = negativeIndex = enableRed = enableBlue = enableGreen = ret = exitStatus = 0; selectIndex = -1; long red, blue, green; red = blue = green = 0; char *colorString, *inputString; char negativeTintInput[10] = ""; /* -l (limit), -i (invert), -s (select) */ while ((i = getopt (argc, argv, ":rghbtlis:c")) != -1) switch(i) { case 'l': optionSwitch = 1; break; case 'i': optionSwitch = -1; break; case 'c': optionSwitch = 2; break; case 'r': enableRed = 1; break; case 'g': enableGreen = 1; break; case 'b': enableBlue = 1; break; case 's': selectIndex = atoi(optarg); break; case 'h': usage(); case 't': terse = 1; break; case '?': /* here we hand number arguments to a string to parse */ /* because we want to do negative args without escaping (--) */ switch(optopt) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': negativeTintInput[negativeIndex++] = optopt; break; default: usage(); } } /* always gonna need an input string. */ if (argv[optind] == NULL) usage(); /* if not inverting, need tint value. */ if (optionSwitch != -1 && optionSwitch != 2 && !negativeIndex) if (argv[optind] == NULL) usage(); /* default to all colors. */ if (!enableRed && !enableGreen && !enableBlue) enableRed = enableGreen = enableBlue = 1; /* set input and color strings, default to last 6 characters. */ inputString = argv[argc - 1]; selectIndex = selectIndex == -1 ? strlen(inputString) - 6 : selectIndex; colorString = &inputString[selectIndex]; /* set color handles. */ red = hexToDec(colorString, 0); green = hexToDec(colorString, 2); blue = hexToDec(colorString, 4); /* set and apply the tint value if we're using it */ if (optionSwitch != -1 && optionSwitch != 2) { tintValue = negativeIndex ? -1 * atoi(negativeTintInput) : atoi(argv[optind]); red += tintValue; green += tintValue; blue += tintValue; } /* do the thing */ switch(optionSwitch) { case -1: /* invert */ red = 255 - red; green = 255 - green; blue = 255 - blue; break; case 0: /* normal */ case 1: /* limit */ ret = makeValid(&red, optionSwitch); ret += makeValid(&green, optionSwitch); ret += makeValid(&blue, optionSwitch); exitStatus = ret >=2 ? 1 : 0; break; case 2: /* contrast - ref: https://24ways.org/2010/calculating-color-contrast/ */ yiq = ((red * 299) + (green * 587) + (blue * 114)) / 1000; exit(yiq >= 128 ? 1 : 0); } /* insert result if enabled */ if (enableRed) decToHex(red, &colorString[0]); if (enableGreen) decToHex(green, &colorString[2]); if (enableBlue) decToHex(blue, &colorString[4]); /* print color only */ if (terse) { colorString[6] = '\0'; inputString = colorString; } printf("%s", inputString); exit(exitStatus); }
/* reads the data of a message into the send buffer */ void handleReadMessage() { char upperNibble = dequeue(&requestBuffer), lowerNibble = dequeue(&requestBuffer); unsigned int index = hexToDec(upperNibble)*16 + hexToDec(lowerNibble); pSmsNode_t pSelectedSms = getSmsWithIndex(index); if(pSelectedSms == NULL) { sendInvalidRequestReply(); return; } enqueueString(&replyBuffer, "MC", 2); enqueue(&replyBuffer, upperNibble); enqueue(&replyBuffer, lowerNibble); if (pSelectedSms->type == INCOMING_MESSAGE) { unsigned int srcLen, i; SMS_DELIVER* pMsg = (SMS_DELIVER*)pSelectedSms->pSMS; /* fetch timestamp time */ enqueue(&replyBuffer, pMsg->timestamp[7]); enqueue(&replyBuffer, pMsg->timestamp[6]); enqueue(&replyBuffer, pMsg->timestamp[9]); enqueue(&replyBuffer, pMsg->timestamp[8]); enqueue(&replyBuffer, pMsg->timestamp[11]); enqueue(&replyBuffer, pMsg->timestamp[10]); /* fetch destination */ enqueue(&replyBuffer, decToHex(DEVICE_ID_LENGTH/16)); enqueue(&replyBuffer, decToHex(DEVICE_ID_LENGTH%16)); char dest[] = DEVICE_ID; for(i=0 ; i<DEVICE_ID_LENGTH ; ++i) { enqueue(&replyBuffer, '0'); enqueue(&replyBuffer, dest[i]); } /* fetch source */ for(srcLen=0; (pMsg->sender_id[srcLen] != (char)NULL) && (srcLen < ID_MAX_LENGTH); ++srcLen); enqueue(&replyBuffer, decToHex(srcLen/16)); enqueue(&replyBuffer, decToHex(srcLen%16)); for(i=0 ; i<srcLen ; ++i) { enqueue(&replyBuffer, '0'); enqueue(&replyBuffer, pMsg->sender_id[i]); } /* fetch SMS message data */ unsigned int dataLength =pMsg->data_length; enqueue(&replyBuffer, decToHex(dataLength/16)); enqueue(&replyBuffer, decToHex(dataLength%16)); for(i=0 ; i<dataLength ; ++i) { enqueue(&replyBuffer, decToHex(pMsg->data[i]/16)); enqueue(&replyBuffer, decToHex(pMsg->data[i]%16)); } } else /* OUTGOING_MESSAGE */ { unsigned int destLen, i; SMS_SUBMIT* pMsg = (SMS_SUBMIT*)pSelectedSms->pSMS; /* no timestamp, send zeros instead (see README) */ enqueueString(&replyBuffer, "000000", 6); /* fetch destination */ for(destLen=0; (pMsg->recipient_id[destLen] != (char)NULL) && (destLen < ID_MAX_LENGTH); ++destLen); enqueue(&replyBuffer, decToHex(destLen/16)); enqueue(&replyBuffer, decToHex(destLen%16)); for(i=0 ; i<destLen ; ++i) { enqueue(&replyBuffer, '0'); enqueue(&replyBuffer, pMsg->recipient_id[i]); } /* fetch source */ enqueue(&replyBuffer, decToHex(DEVICE_ID_LENGTH/16)); enqueue(&replyBuffer, decToHex(DEVICE_ID_LENGTH%16)); char src[] = DEVICE_ID; for(i=0 ; i<DEVICE_ID_LENGTH ; ++i) { enqueue(&replyBuffer, '0'); enqueue(&replyBuffer, src[i]); } /* fetch SMS message data */ unsigned int dataLength = pMsg->data_length; enqueue(&replyBuffer, decToHex(dataLength/16)); enqueue(&replyBuffer, decToHex(dataLength%16)); for(i=0 ; i<dataLength ; ++i) { enqueue(&replyBuffer, decToHex(pMsg->data[i]/16)); enqueue(&replyBuffer, decToHex(pMsg->data[i]%16)); } } }
int main(int argc, char **argv) { int machineCodeFD = -1; // File descriptor of file with object code PC = 0; // The program counter // Verify that the command line has an appropriate number // of arguments if (argc < 2 || argc > 3) { printf("Usage: %s InputFilename [startingOffset]\n", argv[0]); return ERROR_RETURN; } // First argument is the file to open, attempt to open it // for reading and verify that the open did occur. machineCodeFD = open(argv[1], O_RDONLY); if (machineCodeFD < 0) { printf("Failed to open: %s\n", argv[1]); return ERROR_RETURN; } // If there is a 2nd argument present it is an offset so // convert it to a value. This offset is the initial value the // program counter is to have. The program will seek to that location // in the object file and begin fetching instructions from there. if (3 == argc) { // See man page for strtol() as to why // we check for errors by examining errno errno = 0; PC = strtol(argv[2], NULL, 0); if (errno != 0) { perror("Invalid offset on command line"); return ERROR_RETURN; } } printf("Opened %s, starting offset 0x%016llX\n", argv[1], PC); // Start adding your code here and comment out the line the #define EXAMPLESON line FILE *fileStream; fileStream = fopen(argv[1], "rb"); // open file in binary read mode fseek(fileStream, 0, SEEK_END); fileLen = ftell(fileStream); // determine file length fseek(fileStream, 0, SEEK_SET); fileContents = malloc(fileLen * sizeof(char)); // initialize char array of file contents fgets(fileContents, fileLen, fileStream); while (PC < fileLen) { resetInstrBuffer(); // set instruction buffer to zeros fseek(fileStream, PC, SEEK_SET); // set read offset to PC int test = fread(instrBuffer, 1, 1, fileStream); // read one byte from fileStream icode_ifun = instrBuffer[0]; // set icode_ifun var icode_ifun_arr = decToHex(&icode_ifun); // array containing icode and ifun icode = icode_ifun_arr[0]; // extract icode ifun = icode_ifun_arr[1]; // extract ifun fseek(fileStream, PC, SEEK_SET); bytesToRead = getBytesToRead(icode); // get num bytes to read based on icode. Will terminate if invalid icode numBytesCanRead = fread(instrBuffer, 1, bytesToRead, fileStream); // read bytesToRead into instrBuffer // if not enough bytes, set exit condition if (numBytesCanRead < bytesToRead) { notEnoughBytes = TRUE; } processInstr(); } // if PC strictly exceeds length of file, normal termination printf("Normal termination\n"); exit(0); return SUCCESS; }