Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / LONG to CHAR*

Author
Message
CAJUN57
16
Years of Service
User Offline
Joined: 29th May 2008
Location:
Posted: 14th Jun 2008 22:49
could someone please tell me how to convert a long to a char*

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 15th Jun 2008 01:50
Typically you wouldn't as the compiler might complain if you tried. The long is an signed integer type whereas char * is a pointer. A long may have a value stored in it that can be taken as a pointer but you'd have to be pretty sure how it was derived. The long is also signed, which means it can't accurately describe a 4 Gig address space though, again, the compiler might deal with that in conversion.

That said, if you want to make the attempt, you don't actually convert a long to a char *. Instead you'd take a long and cast its value into an existing char *.

long biggienum = 492342342;

char *mypointer = (char *) biggienum;

biggienum gets assigned a value somwhere and mypointer is assigned that value but cast as a (char *). mypointer would then act as a pointer, hopefully with a value that points to a valid character or character string.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
jinzai
18
Years of Service
User Offline
Joined: 19th Aug 2006
Location: USA
Posted: 16th Jun 2008 02:56 Edited at: 16th Jun 2008 02:56
The compiler will absolutely complain, but that is done alot in Windows. For example, GetWindowLong only returns LONGS, but it must return a function pointer when you ask for the WndProc address. Also, the lParam parameter in messages is often a char* (still called an LPSTR, LPCSTR, and many other things.) The cast is safe, unless you are coomplilng for a 64-bit environment. Of course, there is no guarantee for arbitrarily using it, as Lilith has already stated.

The complaint of the compiler can be turned off by going into the project's property pages, under Compiler/General, and selecting No for the option Detect 64-bit Portability Issues.

Login to post a reply

Server time is: 2024-09-29 23:18:41
Your offset time is: 2024-09-29 23:18:41