hay

I am using Orangutan-lib with my orangutan. in that lib, theres a lcd function called lcd_int. that function prints an int number that has only 3 digits. I need to print a larger number. How do I do that?

Yeah, it’s confusing that the lcd_int function in Orangutan-lib takes an integer, but can only handle three digit numbers (in this case the ‘int’ type has 16 bits, so an unsigned int variable can hold numbers from 0 to 65535).

The lcd_int function works like this:

```
void lcd_int(uint16_t n){
char st[4] = {0,0,0,0};
// The 0x30 addition shifts the decimal number up
// to the ASCII location of "0".
// Hundreds place
st[0] = (n / 100) + 0x30;
n = n % 100;
// Tens place
st[1] = (n / 10) + 0x30;
n = n % 10;
// Ones place
st[2] = n + 0x30;
// Print it as a string
lcd_string(st);
}
```

Basically it’s figuring out the hundreds, tens, and ones digits of your number n in base-10, and storing the ASCII characters of those digits in a string to print at the end. You can extend this technique to a function that can handle the full range of an unsigned integer like this:

```
void lcd_full_int(unsigned int n){
unsigned char st[6] = {0,0,0,0,0,0};
// The 0x30 addition shifts the decimal number up
// to the ASCII location of "0".
st[0] = (n / 10000) + 0x30;
n = n % 10000;
st[1] = (n / 1000) + 0x30;
n = n % 1000;
st[2] = (n / 100) + 0x30;
n = n % 100;
st[3] = (n / 10) + 0x30;
n = n % 10;
st[4] = n + 0x30;
// Print it as a string
lcd_string(st);
}
```

The ‘st’ array is always one character longer than the maximum number of digits you intend to print, because a 0 byte at the end tells the lcd_string function to stop printing. You could make neat little tweaks to this function to, say, print a variable length integer without leading zeros, or a signed integer with a ‘-’ sign in front when necessary. You can also throw in a ‘.’ character if you want a decimal point somewhere. Have fun!

-Adam

Thanks!

That helps a lot! I’ll get right to it!