0

I am not familiar to C programming and used to casting some variables to string by ToString() in C#.

But now I need to write a similar function in C for some embedded Linux system.

What is the most general way to do that?

For example:

unsigned short a = 1234;
char* a_string = malloc(5);

char* ToString(unsigned short v)
{
    char* str
    ...
    ...
    return str;
}

int main()
{
    a_string = ToString(a);
}
// result : a_string[0] = '1',  a_string[1] = '2', a_string[2] = '3', a_string[3] = '4', a_string[4] = '\0'

I would appreciate some hint or direction.

ceng
  • 128
  • 3
  • 17
Mao
  • 31
  • 1
  • 5
  • 6
    See `sprintf` and friends. – gspr Aug 31 '20 at 07:58
  • 4
    `char* a_string = malloc(5);` followed by `a_string = ToString(a);` is a potential memory leak. Not to mention that you should generally never use global variables. – Some programmer dude Aug 31 '20 at 07:58
  • 1
    Yeah, `sprintf()` (well, `snprintf()` to be safe!) is the most generic way. – AKX Aug 31 '20 at 07:59
  • To solve your problems visit - https://www.geeksforgeeks.org/what-is-the-best-way-in-c-to-convert-a-number-to-a-string/ – Yashovardhan Singh Aug 31 '20 at 07:59
  • @Someprogrammerdude: OP says he's doing embedded work. Globals certainly have their use in that setting. – gspr Aug 31 '20 at 08:07
  • @gspr I said *generally*, not always. And in the code shown it's not suitable at all. And `malloc` is *generally* discouraged in embedded work. ;) – Some programmer dude Aug 31 '20 at 08:20
  • 1
    @Someprogrammerdude: To be fair, you said *generally never*, whatever that might mean ;-) But let's not get off topic here. We agree that globals should not be used for what OP is trying to achieve here. – gspr Aug 31 '20 at 08:22

1 Answers1

2
char * a_string = malloc(128);
snprintf(a_string, 128, "%u", a);

See the man pages for printf(3) for more information.

In short, printf and friends take a format string that can contain a bunch of placeholders that signify the formatting of the arguments to follow. In the above, we just use %u, which is the format placeholder for an unsigned int. With sprintf we write the result to the buffer in the first argument (a_string) instead of to standard output.

The snprintf variant is for safety, to guarantee that we do not write more than the buffer size (here 128, but you should think about what you want in general). Since C strings are null-terminated, it does not matter that we don't use all of the buffer size. Code interpreting a_string as a string will not consider more of the buffer than what was written by snprintf.

Missing from this answer is error handling: if snprintf returns a value of at least the size argument (here 128), then it had to truncate its output. If it returns negative, an error occurred. You'll want to deal with these cases.

PS: If you're in an embedded setting, and you know the size of the output string at compile time (which you probably know since you know the size of unsigned short), you'll probably want to use an array on the stack for the string buffer instead of heap allocating it.

gspr
  • 11,144
  • 3
  • 41
  • 74