View Full Version : Casting int to LPCWSTR (C++)
Al6200
April 1st, 2007, 07:38 PM
If I have:
int a = 10;
(LPCWSTR)a
I get nothing. How do I properly convert the types. The reason is that I want to display an integer in a messagebox.
MTsoul
April 2nd, 2007, 02:06 AM
Uh what is that? That won't even compile.
LPCWSTR b = a;
I think that would implicitly convert the int to LPCWSTR.
God I hate mfc.
Al6200
April 2nd, 2007, 07:11 AM
Ugh, I'm not using MFC. I'm using Win32, although including MFC wouldn't change the syntax (I wouldn't think so)
MTsoul
April 2nd, 2007, 10:29 PM
Yeah same thing that's what I meant. Does that compile or no?
puppy
April 3rd, 2007, 04:17 AM
I want to display an integer in a messagebox.
casting does not convert types. what you need is sprintf (http://search.msdn.microsoft.com/search/default.aspx?siteId=0&tab=0&query=sprintf)
Al6200
April 3rd, 2007, 07:51 AM
Casting does to convert types! (Really it doesn't).
You see, the cast from (LPCWSTR)anyInteger doesn't return an error, but when I put the LPCWSTRered var in a messageBox nothing is shown in the message box, so clearly my cast doesn't work.
puppy
April 3rd, 2007, 08:08 AM
what casting from type A to type B does is extracting value from variable of type A and putting it into variable of type B. no conversion is done, and that is, in fact, the very purpose of casting.
now, LPCWSTR is Long Pointer to Wide Character STRing. Therefore, writing smth like (LPCWSTR)anyInteger means using anyInteger value as a pointer to a string. since anyInteger does not really point anywhere, there is no string for your message box to display.
on the other hand, sprintf converts its arguments to strings and inserts in your string at required position. you can pass that string to a message box later.
Al6200
April 3rd, 2007, 04:59 PM
Wow, I never knew that.
I guess C# has just wrecked my mind.
But I suppose (LPCWSTR)"a" works because a is a string, and is therefore already a pointer, so making it point to LPCWSTR would make sense...
Thanks, really great, clear, awesome explanation.
Al6200
April 3rd, 2007, 08:13 PM
Okay, I tried the following:
char na [10];
sprintf(na, "%d", 10);
::MessageBox(NULL, (LPCWSTR)na, L"ASDJSDAK", MB_OK);
AND IT BLEW UP IN MY FACE.
The message box just displayed a bunch of empty boxes. I thought casting char* to LPCWSTR was cool because char* is a pointer.
WHAT DO I DO?
You see, I'm working with assembly code and I need a way to output the results of my work...
puppy
April 4th, 2007, 04:24 AM
I dont really have C compiler here so I can't make you sample code. But try one of these (http://www.google.com/codesearch?q=sprintf+messagebox).
Al6200
April 4th, 2007, 07:36 AM
is there any way that I can just trace ints to the screen while debugging like in Flash? I mean, all I'm doing is using Assembly and I want to be able to know what's going on.
λ
April 6th, 2007, 05:47 AM
It's a wide string, not a normal string, so you need to use either MultiByteToWideChar or mbstowcs to convert the string first.
Try this:
char na [10];
whar_t wa [10];
sprintf(na, "%d", 10);
mbstowcs(wa, na, 10);
::MessageBox(NULL, (LPCWSTR)wa, L"ASDJSDAK", MB_OK);
This should work. Not that I've ever dirtied my soul by doing any Win32 programming or anything ;)
puppy
April 6th, 2007, 06:37 AM
@λ:
There are 10 kinds of people in this world:
like "hey w00tz mazz all about",
like "im too lazy to learn this s**t, f**k off",
like "you talking to me, right?",
like "whatever you say goes over my head",
like "i have no time for this, busy busy busy",
like "hax0rz think in binary",
like "real hax0rz think in hexadecimal",
like "de ce what?",
like "what's decimal?",
and, finally, normal people.
Al6200
April 6th, 2007, 10:10 AM
like "hax0rz think in binary"
I think I'm that one since I'm learning to write straight binary:
011001101000101111001000
I think that is the same as
mov ecx, eax
How come some opcodes are 16 bits but others are 8 bits?
Anywho, I think that expression of 24 bits can be expressed as 6 characters of hexidecimals methinks.
2^24 = 2^4 * 2^4 * 2^4 * 2^4 * 2^4 * 2^4 = 16^6
Al6200
April 6th, 2007, 10:15 AM
λ:
It worked, you are my new hero. Now I can pwn n00bs with all sorts of 1337 ASMs and not worry about n00bs.
puppy
April 6th, 2007, 10:30 AM
How come some opcodes are 16 bits but others are 8 bits? as far as i remember there is a prefix byte that turns instruction with "*x" into instruction with "e*x". that's quite different from "*l/*h" -> "*x" transform that actually changes bits in opcodes. well, 2nd was there when CPUs had no "e*x" registers.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.