Convert char object to string

Hi ı am trying to learn arduino.

How ı can convert string object to char object.
This is not working
String object="hello";
char (object)=object;

look at the documentation for the String class. There is a c_str() function that returns the char array pointer.

This is the most important thing to learn about Strings

And then change to PString or SafeString

of course the purists here will intervenate "really learning to program means using correct using of arrays of chars etc."
Well it is the hard way. Nobody has to go the hard way only because somebody else learned it the hard way

So as next step I personally recommend
https://www.arduino.cc/reference/en/libraries/safestring/
best regards Stefan

I disagree.

If you want to learn C or C++ on small microcontrollers, you need to understand the fundamental underlying data representation and functions as well as associated challenges to be aware of. (The libraries you mention are depending on this)

Once you get that, you are equipped with the knowledge necessary to decide if you prefer using a higher level abstraction (and consequences in terms of performance or memory and possible bugs).

If you have no long term interest in learning programming for microcontrollers and it’s a quick throw away school project then in 95+% of the cases using the built in String will work

On larger modern platforms running at multiple GHz, with an OS and gigs of virtual memory then use the higher level built in classes (not the libraries you mention) because they also deal with UTF data representation, locale etc.

1 Like

As the author of the SafeString library, I originally subscribed to the trivialization of Strings put forward by that article but wanted something more robust then char[ ] and strcpy,strcat. More like Strings but based on fixed size char[ ] with lots of error checking and detailed error messages.

However, later on, a closer study has convinced me that Arduino Strings are not that bad.
Fragmentation is not that common and easily avoided and in any case the holes left can and are re-used in spite of what the article suggests.

See my tutorial on Taming Arduino Strings.
In summary

The most 'evil' thing about the article is, in my view, that the author promotes replacing Strings, which don't cause the sketch to crash due to buffer overflows, off by one indexing and missing terminating '\0', with fragile low level c-string methods can easily crash the sketch if not very carefully coded. In particular the author promotes the strcpy and strcat methods which have no protection against buffer overflows. strcat is particularly difficult to use safely in a non-trivial program.

The article would be much less 'evil' if the author had instead suggested using strlcpy and strlcat which, when correctly used, are very safe.

The sketch strlcpy_strlcat.ino provides examples of how to use these method which, while not 'standard' C, are commonly available, and how to detect when the destination char[ ] would have overflowed if it were not for the protections build into the strl.. methods and their PROGMEM counter parts.

When used correctly anything (That is not buggy) is very safe …

I was referring to intrinsic safety. Not safety that depends on a detail analysis of the code path to this statement and calculation of the current contents of the char[ ] that you are concatinating to.

OK would you mind providing example-codes and explain where the limits are?

For PString this is very easy: If you try to store more characters than the String can hold, the too much characters are just dropped.

best regards Stefan

There is no such thing of one size fits all.

The more checks you hand over to the underlying function the more resilient the code and the less knowledge you need to have. But this comes with « strings attached » (always get the performance impact for example even when you don’t need the belt and suspenders).

Having the knowledge of what’s going on and possible risks gives you choice, and that comes handy in many projects where memory or CPU cycles are precious and need to be closely controlled. This gives you more power and of course power comes with responsibility. You have to be diligent.

I’m always advocating for gaining more applicable knowledge. That’s a general path to freedom in life.

If you are referring to Arduino Strings then they just stop adding chars when they run out of memory.
Edit -- The Taming Arduino Strings has a number of examples.

For SafeStrings, I chose if different 'all or nothing' approach. If the addition does not completely fit, none of the chars are added and an error flag is set and if you have error messages enabled you get a detailed message with the name of the SafeString variable, the current contents, and what was trying to be added and why it could not be completely added.
For SafeString, there are lots of example sketches that come with the library, most of which include illustrations of error handling for that particular method.

That’s a key point…

you still need to be diligent and check for completion or error flag after the operation, which most developers don’t do due the false sense of security (consequence of ignorance and /or drinking the snake oil and trusting marketing BS). This is not much different than performing the checks yourself beforehand (and you decide when this is necessary).

Anyway - I’m not starting this conversation again. See previous long threads :slight_smile:

True in that sense Arduino Strings are easier.
And usually no need to check the results for strcat, the program crash will be a big hint something is wrong somewhere in your myriad of code.

:smiley: that’s a way as well … but not recommended

The point is you know you need to ensure there is enough space in the buffer when you call strcat() (because you acquired the knowledge and understand what’s going on) and thus you perform the test if it is necessary.

How else do you check the results for strcat?
Edit -- Ah so you do a detailed path analysis of all the code paths to this statement, then calculate the length (by hand) currently in the char[ ] and then check (by hand) that this strcat will not overflow.
Way more work then I want to do the first time, let alone every time I edit the code.
strlcat does the buffer overflow check for me and much more reliably.

My final statement:

In future I will try to convince newcomers with posting ready to use and easy to understand code. Mentioning traps in the comments.

That will be hard to beat.

best regards Stefan

I think we can agree that There is no magic wand and When this is necessary (*), someone has to write code to check if there is enough room In the buffer and decide what to do if there is not enough space.

So I’m saying that if you have the knowledge of what’s going on doing the test yourself before or after upon delegating that test to a smarter function is not a big deal. You need to test, you need to decide how to handle the edge case anyway.

—-

(*) you might say it’s always necessary to perform the test - and I disagree. Some companies impose to do so because statistically a lame programmer will introduce a bug and that’s OK, it’s a company policy. But when it’s you, yourself and your pet project on à microcontroller, you get to decide what is needed and have the choice to be a lame programmer or a diligent one. Knowledge helps put you in the right camp and thus that’s why I advocate for getting the knowledge. It never hurts to understand what’s going on.

Remember the case recently where There was a String S and an int i and the OP was doing
S += "i=" + i;
If you don’t have the understanding of cString and pointer operations then you are in the dark.

Get the knowledge. Understand the language you are using. That’s key.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.