Home C++ Strings Functions to_wstring() C++ std::to_wstring() Function Definition In C++, std::to_wstring() converts a number to a std::wstring.
Include <string>.
This is a free function from <string> that works with std::string (and sometimes wstring). Member functions like find live on the string object with a dot; helpers like this are called as std::to_wstring(...).
Syntax Pass a numeric value.
std::wstring w = std::to_wstring(42); Call What it does std::to_wstring(val)Numeric value to wide string.
Use when the rest of your API speaks wchar_t. For UTF-8 text, prefer std::string + to_string.
Library declarations wstring to_wstring(int val);
wstring to_wstring(double val);
// ... other numeric overloads Parameters Parameter Description value Numeric value.
Return Value Return Value Description wstringWide decimal representation.
C++ Examples 1. From int 2. From double 3. Length Example 1 — Basic to_wstring() Convert an int to wstring.
#include <iostream>
#include <string>
int main() {
std::wstring w = std::to_wstring(42);
std::wcout << w << L'\\n';
return 0;
} How It Works to_wstring builds a wide string. wcout prints it. Digits appear as 42. Example 2 — to_wstring Double Convert a floating value.
#include <iostream>
#include <string>
int main() {
std::wstring w = std::to_wstring(2.5);
std::wcout << w << L'\\n';
return 0;
} How It Works Default floating format applies. Wide string holds the digits. Trailing zeros may appear. Example 3 — Wide Length Check size of the result.
#include <iostream>
#include <string>
int main() {
std::wstring w = std::to_wstring(100);
std::cout << w.size() << '\\n';
return 0;
} How It Works Text is three wide characters: 1,0,0. size() counts them. Output is 3. Frequently Asked Questions What does to_wstring do? Converts numbers to std::wstring.
Difference from to_string? Element type is wchar_t instead of char.
When use it? When working with wide-character APIs.
Parsing wide strings? stoi/stol/... also have wstring overloads.
Next: memccpy() Keep going through the string functions. Copy bytes until a given byte is copied or n bytes are done.
memccpy() tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion