Jump to content
43oh

Electricity monitor - need some help


Recommended Posts

Chugging along now, but hit a snag. Does anyone have a good routine to turn an unsigned int into its string equivalent (to send to LCD)? This routine throws errors in completely unrelated functions when I add it (commenting it out builds clean) :?

 

/* Takes an int and returns a char[5]
* a 16-bit unsigned int can be a max of 5 characters (65535)
*/ 
char[] int_to_str(unsigned int i)
{
char[5] the_str;
int str_pos = 0;

//parse into char[]
for(str_pos = 0; str_pos < 4; str_pos++)
{
	the_str[str_pos] = i / (10^(4 - str_pos));
	i -= i / (10^(4 - str_pos));
}

return the_str;
}

Link to post
Share on other sites
  • Replies 78
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

@GeekDoc It does work on my board, I am using CCS. Must be debugger.

In IAR Embedded Workbench this code working too without any problems...

Copy Your code into an old, fully functioning project. You may have accidentally overwritten something in the default settings ...

Posted Images

I use sprintf() to do this.

 

Here's an example that I ripped off the net somewhere:

 

#include 
// ^ must include to use sprintf()
// we will start with a simple integer ex.
int main()
{
  char buff[50];
  int ret, a = 34, b = 234;

  ret = sprintf(buff, "%d minus %d equals %d",a,b,a-;

  printf ("(%s) is the result of our sprintf, which is %d characters long",buff,ret);

  return 0;
}

 

Instead of a real string, you could just put your number in there somewhere like this:

 

ret = sprintf( buff, "%d", youruint);

 

Does that help?

Link to post
Share on other sites

How about something like this:

//assume data is an array of your chars and myInt is the int you want to convert
//0x30 is where numbers start in ASCII
unsinged int charPos = 0;
while(myInt > 0) {           
data[charPos] = 0x30 + (myInt % 10);  
myInt /= 10;
charPos++;
}

Link to post
Share on other sites
How about something like this:

//assume data is an array of your chars and myInt is the int you want to convert
//0x30 is where numbers start in ASCII
unsinged int charPos = 0;
while(myInt > 0) {           
data[charPos] = 0x30 + (myInt % 10);  
myInt /= 10;
charPos++;
}

I started that way, but it will reverse the digits:

 

2345 % 10 = 5

234 % 10 = 4

...

 

Going to check through libraries. This is a commonly needed function, so it should be there somewhere.

Link to post
Share on other sites
Well, then you can pass your first position in the array, for example charPos = 10, and then work your way back, charPos--;

Thought of that, too. Problem is that it requires knowing the number of digits in the supplied int, or having a fixed size for the string. ;)

 

As with most programming problems, someone has solved this already. I just have to find where (libraries).

Link to post
Share on other sites

How about this then, a function that will determine the size of the array if(i<10) arraySize=1 else if(i<100) arraySize=2 and so on until you reach max int size.

Another way is to rebuild your initial array afterward since you have the size.

 

unsinged int charPos = 0;
while(myInt > 0) {           
  data[charPos] = 0x30 + (myInt % 10); 
  myInt /= 10;
  charPos++;
}

unsigend int size = charPos;
unsigned int reversed[size];
while(charPos >0){
charPos--;
reversed[charPos] = data[size-1-charPos];
}

Link to post
Share on other sites

Counting number of digits in an int.

 

http://stackoverflow.com/questions/5545 ... tring-cast

 

Since we know its an unsigned 16bit int, so a number ranging from 0 to 65535

 

const unsigned int test[] = { 1, 10, 100, 1000, 10000 };

unsigned int digits = 0;

while(importantnumber >= test[digits]) ++digits;

 

Digits will be 0 for an int with value 0 (or nll or unintialized?) while 1 for 1-9, 2 for 10-99, etc.

Link to post
Share on other sites

cde and Google to the rescue! Turns out that this (sprintf()) is not strictly in the C/C++ language, but is widely implemented in stdio libraries. Also, adding a couple of global char arrays (strings) made things a lot easier.

 

Easy, 4-line solution:

char* int_to_str(unsigned int i, char* the_str)
{
//sprintf() returns # chars, but this does not currently use the info
int total_chars; 
total_chars=sprintf(the_str, "%d", i);
return the_str;
}

 

Result:

post-73-135135494596_thumb.jpg

 

On to capturing and working with the inputs!

 

EDIT: Just noticed I'm up to 1.8K! Going to have to a) hope the rest of the code doesn't add much, b)find ways to trim size, or c) wait for newer, higher capacity value-line chips (still waiting on samples). Probably will go with combo of a) and c). ;)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...