// Inserts leading characters e.g. '0' or '_' in front of an integer Number converted to a string in order to make it to fixed length // intValue = Number (numerical) to be output as string with fixed length // strDest = Destination String that holds the changed result // Base = radix Base of conversion: 2 = binary representation, 10 regularly >10 does not work ! // InsertChar = Character to be inserted in front of converted number // NoDigits = No of characters to be output char * itoa_NoDigits( char * strDest, int intValue, char Base, char InsertChar, char NoDigits) { char firstLoop = true; strDest[(int) NoDigits] = '\0'; // set NULL-character for string end while(NoDigits--) // make number of loops to fill destination string { if( intValue ) // If intValue != 0 then... strDest[ (int) NoDigits] = '0' + (intValue % Base); // fill in destination string - convert from number to ASCII character by addition of '0' else // If intValue == 0 then... { if (firstLoop) strDest[ (int) NoDigits] = '0'; // insert exceptional "0" in case insertChar = blank in order to receive one "0" else strDest[ (int) NoDigits] = InsertChar; // insert regular leading character } intValue /= Base; // prepare for next modulo operation i.e. cut away last digit. firstLoop = false; } return strDest; } // itoa_NoDigits // Converts DS1631 read temperatur to strDest[] prepared to be output as ASCII Text e.g. on a display // Parameter: strDest: prepared character array that holds the result string // intTemp: 2 Byte Integer as read from DS1631 Sensor // DeicimalDigits: Number of decimal digits after comma to be displayed [0, 1, 2, 3, 4] // Return Value: Pointer to string buffer that holds the converted result - without " Grad C" // - works 15.02.10 char * itoa_DS1631_Temp( char * strDest, int intTemp, char DecimalDigits) { unsigned intTempHighByte; // high byte represents integer degrees in front of comma unsigned intTempLowByte; // low byte represents decimal degrees after comma in higher nibble char tmpBuffer[5]; if (intTemp & 0b1000000000000000) // Temperature is negativ { intTemp = (~ intTemp) + 1; // Invers sign by2 complement over both !! bytes strcpy( strDest, "-" ); // set '-' sign } else strcpy( strDest, "+" ); // set '+' sign, what is also used for exactly zero degree intTemp >>= 4; // shift integer value so that used bits are at lowest bit position intTempLowByte = (intTemp & 0b0000000000001111) * 625;// 8 high bits of intTemp are set to zero one level corresponds to 0,0625 switch(DecimalDigits) // add rounding number depending on number of decimals to be displayed { case 0: intTempLowByte += 5000; break; // For rounding effect add: 0,5000 0,0500 0,0050 0,0005 0,0000 case 1: intTempLowByte += 500; break; // ... depending on number of decimal digits to be displayed case 2: intTempLowByte += 50; break; case 3: intTempLowByte += 5; break; //case 4: intTempLowByte += 0; break; // There is nothing to round if all 4 possible decimals are to be displayed } intTempHighByte = intTemp >> 4; // in total shift right intTemp for high byte 8 times if (intTempLowByte >= 10000) // make up for rounding effect that influences high-byte intTempHighByte++; // only required if no decimals are to be displayed strcat(strDest, itoa_NoDigits(tmpBuffer, intTempHighByte, 10, ' ', 3) ); // Convert High byte to ASCII with leading blanks if ( DecimalDigits ) strcat( strDest, "," ); // Place comma only if decimal digits are to be displayed itoa_NoDigits(tmpBuffer, intTempLowByte, 10, '0', 4); // convert decimals to 4 digit incl. leading zeros in front of digits and after comma strncat( strDest, tmpBuffer, DecimalDigits ); // copy requested number of decimal digits to result string return strDest; // return pointer of composed string } // itoa_DS1631_Temp