Reputation: 1027
I am in new to C++ on windows. Can you please tell me how to convert unsigned int
to TCHAR *
?
Upvotes: 2
Views: 14288
Reputation: 110
TCHAR buf[5];
memset(buf, 0, sizeof(buf));
return _itot_s(num, buf, 10);
Upvotes: 0
Reputation: 320
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;
}
Upvotes: 0
Reputation: 67256
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 ) ;
Upvotes: 5
Reputation: 56499
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
.
Upvotes: 1
Reputation: 1708
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.
Upvotes: 0