Jump to content
43oh

Cannot understand code snippet


Recommended Posts

Can anyone do a better job explaining this code to me? it is from a launchpad script that hooks a launchpad up to a serial LCD from sparkfun. It has to do with packing data to send via UART to the LCD but I cannot understand it. 

 

Thanks 

// base-10 itoa for positive numbers. Populates str with a null-terminated string.
// limit lets you overflow back to 1 when you hit the limit+1, 2*limit+1, ...
// make sure *str is an array that can hold the number of digits in your limit + 1.
void itoa(unsigned int val, char *str, unsigned int limit)
{
  int temploc = 0;
  int digit = 0;
  int strloc = 0;
  char tempstr[5]; //16-bit number can be at most 5 ASCII digits;

  if(val>limit)
    val %= limit;

  do
  {
    digit = val % 10;
    tempstr[temploc++] = digit + '0';
    val /= 10;
  } while (val > 0);

  // reverse the digits back into the output string
  while(temploc>0)
    str[strloc++] = tempstr[--temploc];

  str[strloc]=0;
}
Link to post
Share on other sites

ok, i understand it.

 

the function is an integer to ascii converter so it takes 12345 as a number and encodes it as the text characters '1' , '2' , '3' , '4' , '5'

// base-10 itoa for positive numbers. Populates str with a null-terminated string.
// limit lets you overflow back to 1 when you hit the limit+1, 2*limit+1, ...
// make sure *str is an array that can hold the number of digits in your limit + 1.
void itoa(unsigned int val, char *str, unsigned int limit)  //  <-- function takes an integer as an input and transforms it into a proper string for output.  *str is the string that it will modify as an output.
{
int temploc = 0;
int digit = 0;
int strloc = 0;
char tempstr[5]; //16-bit number can be at most 5 ASCII digits;

if(val>limit)
val %= limit;  // % is a modulus command it basically means "what's left over" think long division.  so say 26 / 10 = 2 with a 6 as remainder  well the whole number portion is ignored, and the remainder is the answer...  so 26 % 10 = 6
//  this line clips the input to a known maximum value  useful when your output display only has a set number of digits
do
{
digit = val % 10;  //clip the input number down to just the first digit (remember anything above 10 is clipped and rolls over)
tempstr[temploc++] = digit + '0'; set that position of the string to that single digit value, as well as +'0' to build up the string.
val /= 10; //  cut the bottom digit off the number (this is integer math so there are no decimals)
} while (val > 0);  //as long as there are any numbers left, wash rinse repeat this cycle to encode the entire number into the string.

// reverse the digits back into the output string
while(temploc>0)  //  the temploc variable built up as the temp string was built during the previous step now for each character (which added 1 to temploc)
str[strloc++] = tempstr[--temploc]; //copy the character at each position from the temp string into the output string.

str[strloc]=0; // place a 0 at the end of the output string as a null terminator
}

hope that helps, or if you need anything else explained or expanded on :smile:

Link to post
Share on other sites

If the number is above limit, the number "wraps" over the limit. So if you set limit to 99, you guarantee that the result will be a two digit number.

Then the number is iteratively divided by 10, resulting in a single digit remainder, starting from the least significant digit.

As a string is built with the most significant digit first, the string is built iteratively by counting back through the result indexes.

 

By adding '0' (the character zero) to a number, this results in the corresponding character for that digit.

 

As a last step, the "string terminator" must be added to the end of the string, this is the character 0 (not '0', which represents the number zero, but the zeroeth character  - also called null - is the string terminator)

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...