I am in new to C++ on windows. Can you please tell me how to convert unsigned int
to TCHAR *
?
-
1Do want to convert a value to a pointer!? `unsigned int` is a variable bind to a value and `TCHAR*` is a pointer to a `TCHAR`, many conversations could be applied to them. – masoud May 07 '13 at 16:48
-
1Please describe the particular conversion that you require. Convert an integer to a string representation. Decimal? Hex? – David Heffernan May 07 '13 at 16:51
-
He's trying to _print_ a number into a string. – bobobobo May 07 '13 at 17:07
-
possible duplicate of [Alternative to itoa() for converting integer to string C++?](http://stackoverflow.com/questions/228005/alternative-to-itoa-for-converting-integer-to-string-c) – Raymond Chen May 07 '13 at 17:49
5 Answers
The usual way is to use swprintf
to print wide characters into a wchar_t
(which TCHAR
is normally defined as).
To print numbers into a TCHAR
, you should use _stprintf
as @hvd mentions below (in a fit of rage). This way if UNICODE
is defined you will use wide characters, and if UNICODE
is not defined you will use ASCII characters.
int myInt = 400 ;
TCHAR buf[300] ; // where you put result
_stprintf( buf, TEXT( "Format string %d" ), myInt ) ;

- 64,917
- 62
- 258
- 363
-
2
-
I'm not sure but why you think `swprintf` can convert a `wchar_t*` to a `TCHAR*` properly? – masoud May 07 '13 at 17:26
-
1`TCHAR` is defined as `wchar_t` when `UNICODE` is defined, and Ascii `char` otherwise. Check out your `Windows.h` – bobobobo May 07 '13 at 17:31
-
1@bobobobo Right, so when `UNICODE` is not defined, the first two arguments of `swprintf` have type `(const) char *`, and you get a compiler error at best, a compiler warning and crash at runtime at worst. When `UNICODE` is not defined, you should be calling `sprintf`, not `swprintf`. I cannot confirm it right now, but according to the documentation, you should be using `_stprintf`. – May 07 '13 at 17:42
-
@hvd Hi man, the WinAPI works. If `TCHAR` is `#defined` as `char`, then `buf` is `char[300]`, and `TEXT("string")` dissolves to just `"string"` (so the format string is ascii). `swprintf` then functions like `sprintf`. If `TCHAR` is `wchar_t`, then `TEXT("your string")` turns into `L"your string"` which is a wide character string. – bobobobo May 07 '13 at 17:51
-
The whole point of `TCHAR` was to make wide character / ascii mode compilation just a compiler switch away (`#define UNICODE`). – bobobobo May 07 '13 at 17:57
-
I may have made a slight bad here. You may need to define a macro for `swprintf` to function correctly. Edited above – bobobobo May 07 '13 at 17:59
-
"`swprintf` then functions like `sprintf`" -- No, it doesn't. The "w" in swprintf is for "wide". It always takes `wchar_t *` arguments, regardless of whether `UNICODE` is defined. Your code fails to compile when `UNICODE` is not defined with "error C2665: 'swprintf' : none of the 2 overloads could convert all the argument types". – May 07 '13 at 18:01
-
My slight bad. I forgot you need to define a macro to get it to work that way. – bobobobo May 07 '13 at 18:02
-
1@bobobobo Like I said, there's the `_stprintf` (in `
`) that accepts `TCHAR *`s that forwards to `sprintf` or `swprintf` depending on whether `UNICODE` is defined. (I've actually tested it now, and two other issues: you should use the other overload that takes the buffer size as an extra parameter for security, and `TEXT` should be `_TEXT`.) – May 07 '13 at 18:02 -
Well that's just paranoia. -- Sent from my Mac machine. Now chill out dude. – bobobobo May 07 '13 at 18:03
-
Sorry if I came across upset, I assure you I am not. :) I was merely trying to be as verbose as possible to leave little room for misunderstandings. – May 07 '13 at 18:07
Maybe you want to convert a unsigned int
to a string. You can use std::to_wstring
if TCHAR
is defined as a WCHAR
:
unsigned int x = 123;
std::wstring s = std::to_wstring(x);
Then convert s.c_str()
to a TCHAR*
.
Also you should take a look to MultiByteToWideChar
.

- 55,379
- 16
- 141
- 208
-
@bobobobo: This way is 10x better than using `printf`. `to_wstring` can't overflow its buffer, because the buffer size is adjusted automatically. – Ben Voigt May 07 '13 at 18:05
-
@BenVoigt You can always use the [`*_s` version of these functions](http://msdn.microsoft.com/en-US/library/ce3zzk1k(v=vs.80).aspx) – bobobobo May 07 '13 at 18:08
-
1@bobobobo: That still doesn't make the buffer the correct size, it just makes sure that if the buffer is too short, it fails instead of creating a buffer overrun vulnerability. – Ben Voigt May 07 '13 at 18:30
You can either set the location pointed by the TCHAR*. (Probably a bad idea...)
unsigned int ptr_loc = 0; // Obviously you need to change this.
TCHAR* mychar;
mychar = ptr_loc;
Or you can set the value of the TCHAR pointed to by the pointer. (This is probably what you want. Though remember that TCHAR could be unicode or ansi, so the meaning of the integer could change.)
unsigned int char_int = 65;
TCHAR* mychar = new TCHAR;
*mychar = char_int; // Will set char to 'A' in unicode.

- 1,698
- 1
- 15
- 26
i might be too late but this program might help even in visual studio
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
#pragma comment(lib, "User32.lib")
int _tmain(int argc, TCHAR *argv[])
{
int num = 1234;
TCHAR word[MAX_PATH];
StringCchCopy(word, MAX_PATH, TEXT("0000"));
for(int i = 3; i >= 0 ;i--)
{
word[i] = num%10 + '0';
num /= 10;
}
_tprintf(TEXT("word is %s\n"), word);
return 0;
}

- 180
- 2
- 11