Go Down

Topic: Convert a decimal number into a String? (Read 3624 times) previous topic - next topic


Aug 22, 2009, 05:04 am Last Edit: Aug 22, 2009, 07:14 am by timichango Reason: 1
Hey there, I'm trying to build a basic counter as a test for figuring out how to interface with multiple 7-segment LEDs.

I've got a basic setup running as per the tutorial on the arduino site, and can send arbitrary character bytes to both LEDs, and have created a basic lookup table for characters 0 to 9, and blank (all segments off).

What I'm trying to do now is increment a variable (the counter), and extrapolate the appropriate bytes to shift out based on the individual numerals that make up the counter decimal number.

For example, if I'm trying to write the number "72" to the LEDs, I'd like to  be able to convert the number 72 to a string, grab the digits one-by-one using some kind of string operation, convert each single digit individually back into an integer,  use that integers to assign the appropriate byte from the lookup table, and repeat until all of the digits have been shifted into the shift registers.

Problem is (and I find this SUPER surprising), I'm discovering that Arduino has no convenient way to either:  a) convert an integer to a string, or b) grab a substring of a given string.  

Most of the programming I've done has been using flash/actionscript and I guess I sort-of took for granted that Arduino/C++ would have similar string operation functions and/or integer to string conversion (arduino seems to map converted integers to an ascii character value or something instead of converting the digits literally)

Which leads me to 3 possible conclusions:

1. I'm missing something with regards to working with ints/strings in the reference material

2. I'm thinking about this problem all wrong, and there's another, perfectly logical and straightforward way to take a decimal number and extrapolate the appropriate bytes to send to the shift registers

3.  Both of the above..!

Off the top of my head it occurs to me that I might be able to solve this problem by running a series of cascading incrementing variables that count from 0-9 (one variable for each shift numeral I'm trying to display), but that seems a bit... hacky and indirect.  Being able to display an arbitrary multi-digit integer directly seems a lot more elegant and useful.

Any advice would be deeply appreciated!

-T   :-?
Safety Third!


Problem is (and I find this SUPER surprising), I'm discovering that Arduino has no convenient way to either:  a) convert an integer to a string, or b) grab a substring of a given string.

Problem is, the Arduino has very little memory, and a full implementation of sprintf, with all those left/right justification formats, hex/decimal/string formats is very memory-hungry, as is general string handling. But, it does have these functions.

However, yes, there are other ways around these, not at all hacky.
Constant tables spring to mind.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


There is no need to use strings for this. Converting an integer to an array of individual digits can be accomplished as follows:

Code: [Select]

#define NUM_DIGITS 4

// table of 7-segment patterns
const byte seg_def[]  = // segment patterns for digits 0-9
   B0111111,  // '0'
   B0000110,  // '1'
   B1011011,  // '2'
   B1001111,  // '3'
   B1100110,  // '4'
   B1101101,  // '5'
   B1111101,  // '6'
   B0000111,  // '7'
   B1111111,  // '8'
   B1100111   // '9'

byte digit_array[NUM_DIGITS];

void NumberToArray(word number)
 char i;

 // initialize all digits to the empty pattern (no segments lit)
 for (i=0;i<NUM_DIGITS;i++) digit_array[i]=0;

 // extract each digit (right to left)
 for (i=NUM_DIGITS-1;i>=0;i--) {
   // extract least significant digit as an index into the
   // segment pattern table
   // get next digit
   // exit if there are no more digits to avoid leading zeroes
   if (number==0) break;


Thanks so much for the help - the code above works marvelously... I always forget about modulo!  This was extremely easy to adapt to my needs.

Cheers! :D
Safety Third!


I am expieriencing with the same problem....please spend some time to read my thread so you can help me...
I am rookie and I need it for a project at my university and I need to finish it as soon as possible...

The thing is that i have a byte and I cannot print it to my lcd, because (i think) it contains 2 digit numbers...And it prints some strange characters.
I am explaining everything to my thread...

I am looking forward to your replies!


thanks for this post,  i like it ....

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131