C ++ windows如何将'unsigned int'转换为'TCHAR *'?

时间:2013-05-07 16:45:58

标签: c++ windows int tchar

我是Windows上的C ++新手。您能否告诉我如何将unsigned int转换为TCHAR *

5 个答案:

答案 0 :(得分:4)

通常的方法是使用swprintf将宽字符打印到wchar_tTCHAR通常定义为)。

要将数字打印到TCHAR,您应该使用_stprintf作为下面的@hvd提及(在愤怒中)。这样,如果定义UNICODE,您将使用宽字符,如果未定义UNICODE,您将使用ASCII字符。

int myInt = 400 ;
TCHAR buf[300] ; // where you put result
_stprintf( buf, TEXT( "Format string %d" ), myInt ) ;

答案 1 :(得分:1)

也许您想将unsigned int转换为字符串。如果将TCHAR定义为WCHAR

,则可以使用std::to_wstring
unsigned int x = 123;

std::wstring s = std::to_wstring(x);

然后将s.c_str()转换为TCHAR*

另外,您应该查看MultiByteToWideChar

答案 2 :(得分:0)

您可以设置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.

答案 3 :(得分:0)

我可能为时已晚,但这项计划甚至可能对视觉工作室有帮助

#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;
}

答案 4 :(得分:0)

TCHAR buf[5];
memset(buf, 0, sizeof(buf));
return _itot_s(num, buf, 10);

See this MSDN Link