我在Windows上是C ++的新手。 你能告诉我如何将unsigned int
转换为TCHAR *
吗?
也许你想要将一个unsigned int
转换为一个字符串。 如果TCHAR
被定义为WCHAR
则可以使用std::to_wstring
:
unsigned int x = 123; std::wstring s = std::to_wstring(x);
然后将s.c_str()
转换为TCHAR*
。
你也应该看看MultiByteToWideChar
。
通常的方法是使用swprintf
将宽字符打印到wchar_t
(通常将TCHAR
定义为)。
要将数字打印到TCHAR
,您应该使用下面的_hprintf作为@hvd提及(适合愤怒)。 这样,如果UNICODE
被定义,你将使用宽字符,如果没有定义UNICODE
你将使用ASCII字符。
int myInt = 400 ; TCHAR buf[300] ; // where you put result _stprintf( buf, TEXT( "Format string %d" ), myInt ) ;
您可以设置TCHAR *指向的位置。 (可能是一个坏主意…)
unsigned int ptr_loc = 0; // Obviously you need to change this. TCHAR* mychar; mychar = ptr_loc;
或者你可以设置指针指向的TCHAR的值。 (这可能是你想要的,虽然记住TCHAR可能是unicode或者ansi,所以整数的含义可能会改变。)
unsigned int char_int = 65; TCHAR* mychar = new TCHAR; *mychar = char_int; // Will set char to 'A' in unicode.
我可能已经太晚了,但是这个程序甚至可以在视觉工作室中帮助
#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; }