replacing characters in char array with integers

Hello all. I'm an utter newbie to C, and I am using a third-party library to display characters on a 7-segment display. The library is SevSeg by Dean Reading, on GitHub at GitHub - DeanIsMe/SevSeg: Seven segment display controller library for Arduino .

The library allows display of characters and digits, and I'm using the library to display an output of mixed characters and digits, but the limitation is that if I choose to display characters, I must choose to display the entire output as characters. Which means, converting integers to characters.

The library uses an array of characters, instead of a string. That's just the way the library is written.

What I am hoping to do, is replace the last two characters in the array with two integers.

The example provided by the library developer uses strcpy to load the character array, like this:

** **strcpy(displaystring, " C.59");** **

and the output would be:

** ** C.59** **
on the seven-segment display.

The syntax to display is this:

** ** sevseg.setChars(displaystring);** **

wherein displaystring isn't a string, it's an array of chars.

So, how would I replace the [b]59[/b] in that example with two characters from a two-digit integer?

Many thanks,

S.

Have a look at all the nice functions working on cStrings in stdlib.h and string.h

To build your buffer you could use sprintf(). It uses a lot of memory and alternative with strcat() and itoa() would take less memory but might be more complicated to get started with

char buffer[10]; // ensure it’s large enough for your largest message with 1 extra byte for the trailing NULL char ‘\0’
sprintf(buffer,”C.%d”, 32); // buffer will be “C.32”
sevseg.setChars(buffer);

We don’t recommend the String class but if you want a simple code (with risks depending on how you use this over time) then build a String and then ask for its c_str(), this is the cString buffer you need

String aCrappyString = String(“C.”) + String(32);  // “C.32”
sevseg.setChars(aCrappyString.c_str());

wrgarou:
The example provided by the library developer uses strcpy to load the character array, like this:

** **strcpy(displaystring, " C.59");** **

....

So, how would I replace the [b]59[/b] in that example with two characters from a two-digit integer?

Well, you could use the direct approach.

// We will treat our character string as an array.
// For that, we need to know the array indices.
// Remember, array indices start with zero.

//      Array indices: 012345
strcpy(displaystring, " C.59");

// We see that the characters for our tens digit and ones digit
// will need to go into positions 3 and 4, respectively.

// This is the number we want to display:
byte myNumber = 60; 

// We split the number into its digits, thus:
byte myNumberTens = (myNumber / 10);  // tens digit
byte myNumberOnes = (myNumber % 10);  // ones digit

// Now we have the digit values.
// To convert them to characters, we need to add '0' to each of them.
// (The single quotes around the '0' signify that it is a character.)
// As we convert each digit to a character, we insert it into its
// appropriate place in the string, thus:
displaystring[3] = '0' + myNumberTens;
displaystring[4] = '0' + myNumberOnes;

(Yes, I know that I could have done that in fewer lines of code. But, I broke it down, step by step, so you can have a better idea of what's going on.)

Thank you, Odometer! That's a simple solution that I fully understand. And it works fully in my application.

Yes indeed - That Will work too

Constraint is that you need to have max 2 digits in your variable and if you want C.05 in case your value is 5. If your value is 105 you’ll have a weird output (but I assume you have only 4 seven segments modules and thus know the number is between 0 and 99)

You should read about some of the functions in the libs I pointed at, they will come handy one day and there is nothing rocket science about them