What would be an efficient, portable way to convert a unsigned short to a char* (i.e. convert 25 to '25').
I'd like to avoid things such as getting (std::string) strings involved. Performance is important in this case since this conversion will need to happen quickly and often.
I was looking into things such as using sprintf but would like to explorer any and all ideas.
First off, do it right, then do it fast--only optimize if you can see for certain that a piece of code is not performant.
snprintf()
into a buffer will do what you want. Is it the fastest possible solution? Not at all. But it is among the simplest, and it will suffice to get your code into a working state. From there, if you see that those calls to snprintf()
are so laborious that they need to be optimized, then and only then seek out a faster solution.
An array of strings such that
array[25] = "25";
array[26] = "26";
array[255] = "255";
maybe? You could write a small program that generates the table source code for you quite easily, and then use this file in your project.
Edit: I don't get what you mean by you don't want to ge strings involved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With