Click here to Skip to main content
16,004,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is probably a stupid question but i have not managed to find a working solution to convert an integer to a string so as to display the value on a screen

What I have tried:

I put this code in my source to first make a test with a static value but i plan later to make it a variable.

uint32_t i = 88;//Manual Test
auto charge = std::to_string(i);//Conversion from Figure to String
SimpleTextDraw(lcd_get_width() / 2, lcd_get_height() / 4 + 35, charge, WHITE_TEXT, 100, LAYER_FRONT);

this gives me 2 errors

no suitable conversion function from 'std::string' to 'constant char' exists
and
cannot convert 'std::string' to 'constant char'
Posted

Quote:
no suitable conversion function from 'std::string' to 'constant char' exists
and
cannot convert 'std::string' to 'constant char'

use the c_str method, i.e.
C++
SimpleTextDraw(lcd_get_width() / 2, lcd_get_height() / 4 + 35, charge.c_str(), WHITE_TEXT, 100, LAYER_FRONT);
See, for instance std::basic_string<CharT,Traits,Allocator>::c_str - cppreference.com[^]
 
Share this answer
 
Comments
Carbonkevlar13 30-Jul-24 10:47am    
Thanks but the c_str method is not very clear for me
You have the following lines:
C++
auto charge = std::to_string(i);//Conversion from Figure to String
SimpleTextDraw(lcd_get_width() / 2, lcd_get_height() / 4 + 35, charge, WHITE_TEXT, 100, LAYER_FRONT);

In the first line the call to std::to_string returns (and sets charge equal to) a new std::string. In the second line the third parameter, according to the error message, is expected to be a constant char. So you cannot pass a string when it expects a character.
 
Share this answer
 
Comments
Carbonkevlar13 30-Jul-24 10:46am    
Thanks Richard although that does not help me much..
Richard MacCutchan 30-Jul-24 11:04am    
What do you mean, that does not help me much.? Do you not understand the difference between a std::string and a char type, or is it somethng else. Also we are working in the dark somewhat as all we have is two lines of code out of context. So please provide the actual definition of the SimpleTextDraw function.
merano99 30-Jul-24 12:37pm    
He probably doesn't know how to convert the result into a const char*.
Richard MacCutchan 30-Jul-24 12:39pm    
Or into a "constant char" (without the asterisk). But as so often we only get half the story and have to make guesses.
merano99 30-Jul-24 13:12pm    
Since it converts the number 88 into a string and passes the result to a function, I assumed more than one character. I think that a char* is needed. The prototype of the function would help here, but unfortunately, as so often, important details are missing.
To access the content of a std::string, you normally use the c_str() method. This returns a const char*, and the immutable text cannot be used as a mutable char* without explicit type conversion. Since explicit type conversion is not recommended, the best option is to copy the text into a mutable char*.
C++
auto charge = std::to_string(i); // Conversion from number to string
char* str = new char[charge.size() + 1];
strcpy_s(str, charge.size() + 1, charge.c_str());
SimpleTextDraw(lcd_get_width() / 2, lcd_get_height() / 4 + 35, str, WHITE_TEXT, 100, LAYER_FRONT);
delete[] str; // Free up memory!
 
Share this answer
 
Comments
Carbonkevlar13 30-Jul-24 10:45am    
Thanks. This works although as a newbie i do not understand what it does.
I thought that in the first line "charge" would contain the text "88" coming from i=88 above
What are the other lines doing ?
Carbonkevlar13 30-Jul-24 10:48am    
Can i now use this inside a loop where i will vary from 0 to 100 so that i could display this as a counter or a coutdown ?
merano99 30-Jul-24 12:33pm    
Even as a newcomer, you can't avoid reading the documentation, e.g. on strings. The string object “charge” contains the text “88”, correct, but your function seems to require an old (non-constant) char pointer. It would be much easier if you could change the function to const char*, but this way there is a type conflict that my code tries to work around. Of course you can use the code inside a loop. If you know the maximum length it would also be possible to request the memory only once with new and release it again at the end.
Carbonkevlar13 1-Aug-24 4:27am    
Even when i read the documentation it is not always clear. Thanks
To manage the memory automatically and prevent memory leaks, you could use a std::unique_ptr (instead of new and delete).

C++
// Conversion to a mutable char* 
auto charge = std::to_string(i); // Conversion from number to string
auto str = std::make_unique<char[]>(charge.size() + 1);
std::copy(charge.begin(), charge.end(), str.get());

SimpleTextDraw(lcd_get_width() / 2, lcd_get_height() / 4 + 35, str.get(), WHITE_TEXT, 100, LAYER_FRONT);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900