Advertisement
abdullahkahraman

Char to String

Apr 13th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. /*
  2.  *  Create a function that will return a string.
  3.  *  It accepts 'inputValue' that is up to 255, but you can make it an int or longint...
  4.  *  ...after you make some edits in the function.
  5.  *  inputValue:   5       7       6
  6.  *  Digits:      1st     2nd     3rd
  7.  */
  8. unsigned char* returnString(unsigned char inputValue)
  9. {
  10.     static unsigned char processedString[4]; // Return a string of 3 digits.
  11.     unsigned char firstDigitCounter = 0; // Brute-force counter for first digit.
  12.     unsigned char secondDigitCounter = 0; // Brute-force counter for second digit.
  13.     if (inputValue > 99) // If we have a 3 digit number,
  14.     {
  15.         while (inputValue > 99) // Until our number is 3 digits, i.e. bigger than 99,
  16.         {
  17.             inputValue -= 100; // Subtract 100 and..
  18.             firstDigitCounter++; //.. increment first digit.
  19.         }
  20.         while (inputValue > 9) // Until our number is 3 digits, i.e. bigger than 99,
  21.         {
  22.             inputValue -= 10; // Subtract 10 and..
  23.             secondDigitCounter++; //.. increment second digit.
  24.         }
  25.  
  26.         // Now, we have left the 'inputValue' as a single digit.
  27.  
  28.         processedString[0] = firstDigitCounter + 0x30; // First digit
  29.         processedString[1] = secondDigitCounter + 0x30; // Second digit
  30.         processedString[2] = inputValue + 0x30; // Third digit
  31.         processedString[3] = '\0'; // String terminator.
  32.     }
  33.     else // If we have a 2 digit number,
  34.     {
  35.         while (inputValue > 9) // Until our number is 3 digits, i.e. bigger than 99,
  36.         {
  37.             inputValue -= 10; // Subtract 10 and..
  38.             secondDigitCounter++; //.. increment second digit.
  39.         }
  40.         processedString[0] = secondDigitCounter + 0x30; // Second digit
  41.         processedString[1] = inputValue + 0x30; // Third digit
  42.         processedString[2] = '\0'; // String terminator.
  43.     }
  44.     return processedString; // Return the processed string.
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement