What is the clean way to do that in C?
wchar_t* ltostr(long value) {
int size = string_size_of_long(value);
wchar_t *wchar_copy = malloc(value * sizeof(wchar_t));
swprintf(wchar_copy, size, L"%li", self);
return wchar_copy;
}
The solutions I came up so far are all rather ugly, especially allocate_properly_size_whar_t uses double float base math.
The maximum number of digits is given by
ceil(log10(LONG_MAX)). You can precompute this value for the most common ranges oflongusing the preprocessor:Now, you can use
to get a stack-allocated wide-character string. For a heap-allocated string, use
wcsdup()if available or a combination ofmalloc()andmemcpy()otherwise.