Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Create a function that will return a string.
- * It accepts 'inputValue' that is up to 255, but you can make it an int or longint...
- * ...after you make some edits in the function.
- * inputValue: 5 7 6
- * Digits: 1st 2nd 3rd
- */
- unsigned char* returnString(unsigned char inputValue)
- {
- static unsigned char processedString[4]; // Return a string of 3 digits.
- unsigned char firstDigitCounter = 0; // Brute-force counter for first digit.
- unsigned char secondDigitCounter = 0; // Brute-force counter for second digit.
- if (inputValue > 99) // If we have a 3 digit number,
- {
- while (inputValue > 99) // Until our number is 3 digits, i.e. bigger than 99,
- {
- inputValue -= 100; // Subtract 100 and..
- firstDigitCounter++; //.. increment first digit.
- }
- while (inputValue > 9) // Until our number is 3 digits, i.e. bigger than 99,
- {
- inputValue -= 10; // Subtract 10 and..
- secondDigitCounter++; //.. increment second digit.
- }
- // Now, we have left the 'inputValue' as a single digit.
- processedString[0] = firstDigitCounter + 0x30; // First digit
- processedString[1] = secondDigitCounter + 0x30; // Second digit
- processedString[2] = inputValue + 0x30; // Third digit
- processedString[3] = '\0'; // String terminator.
- }
- else // If we have a 2 digit number,
- {
- while (inputValue > 9) // Until our number is 3 digits, i.e. bigger than 99,
- {
- inputValue -= 10; // Subtract 10 and..
- secondDigitCounter++; //.. increment second digit.
- }
- processedString[0] = secondDigitCounter + 0x30; // Second digit
- processedString[1] = inputValue + 0x30; // Third digit
- processedString[2] = '\0'; // String terminator.
- }
- return processedString; // Return the processed string.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement