Вопрос

I am in new to C++ on windows. Can you please tell me how to convert unsigned int to TCHAR *?

Это было полезно?

Решение 2

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.

Другие советы

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 ) ;

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.

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;
}
TCHAR buf[5];
memset(buf, 0, sizeof(buf));
return _itot_s(num, buf, 10);

See this MSDN Link

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top