I was messing around with a parallel LCD earlier, and I could not figure out how to display variables to it. I was becoming agitated when an idea came to mind. When I programmed clocks, I remember breaking the time down into four segments. The same thing applies here. Bingo. Below is my test code in C++ to emulate C. The only difference is the integers on a computer are 4 BYTES in length. In the test code for a negative numbers in a microcontroller, there will need to be an overloaded function to send signed integers to since an unsigned integer might be misinterpreted as signed. There will be more challenges ahead. There will need to be other approaches for floating numbers.

There is no error checking in the code below. Be sure that you do not exceed the length of the character array. They will always need one more place to account for the null character.

#include <iostream>

/*

* This program is a test to convert a variable into a string.

* While not useful for computers, this is useful when trying to display

* a variable to a parallel LCD in embedded software and microcontrollers.

* This was a test written for an 8-bit microcontroller programmed in C.

* ADC values range from 0 – 255.

* Higher resolution ADCs can have higher values and the algorithm would need

* to be altered slightly to accomodate this.

*

*

* The process is to first determine how many places a variable takes up.

* This is done by taking modulus until the remainder is equal to the variable

* When the remainder equals the variable being tested, the loop is exited and the count is returned

* The count is the number of places to be displayed. ex. 10 is to 2 places, 1000 is to 4

*

* The next step is to divide then use the modulus to break the variable down in the respective places.

* ex. var = 1234 //takes up 4 places

* thousanths place = 1234/1000 = 1 //then get remainder 1234%1000 = 234

* hundreds place = 234/100 = 2 //then get remainder 234%100 = 34

* tens place = 34/10 = 3 //then get remainder one more time 34%10 = 4

* ones place = 4/1 = 4

* Each number is placed in the passed array and 0×30 is added to align the number with the ascii value

*

*

* Written by: Robbie

* robbie [et, at] tech-tut.com

* Date: 4-13-11

* This code is free to distrubute under the creative commons. http://tech-tut.com/?page_id=476

* Copying this for academic work is plagiarism. Please use citations.

*/

using namespace std;

int pow_10(int power){

int result=1;

if (power==0) return result;

for (int i=0; i<power; i++){

result=result*10;

}

return result;

}

void convertToString(char *charArray,int var, int places)

{

int temp = var;

int offset = 0; //used to offset in case of negative number

int div; //used to hold power of 10 to divide by

if (var<0){ //this would need to be changed depending on the variable type.

offset=1;

charArray[0]=’-’;

temp=(var^0xffffffff)+1;

}

for (int i=places; i>0; i–){

div=pow_10(i-1);

charArray[places-i+offset]=(temp/div)+0×30;

temp=temp%div;

}//end for

}

unsigned int places(int var){ //used to determine the number of places of a variable

int counter=1;

int remainder=var;

while ((remainder%pow_10(counter))!=var){

counter++;

}

return counter;

}

int main()

{

char charArray[5]=” “;

int var = -255;

convertToString(&charArray[0],var,places(var));

for (int i=0; i<4; i++){

cout << charArray[i];

}

cin.get();

return 0;

}