Sign in to follow this  
taguilo

PCSTRING data type conversion

Recommended Posts

Hi,Do you C++ experts know whether it is possible to convert regular variable values (ie of type int, float, string, etc) into a PCSTRINGZ data type and vice versa, in Visual C++?. As it is a typedef struct, I cannot find a way to do a direct conversion, and I'm not good with pointers' manipulation (yet).ThanksTom

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

Well, as the name PCSTRINGZ implies, this is a pointer to a string (to be more accurate, its a const pointer to a STRINGZ, which is just another name for a TCHAR array). You couldn't just convert ints or floats to this type, you would need to allocate a TCHAR buffer, use some printf or FormatString type api to convert your data to a string and then pass the TCHAR buffer.

Share this post


Link to post
Share on other sites

Ok... quick lesson. ;)char stringVariable[10]; // <-- an array of characters, up to 9 with a terminating zero value.char* pstringVariable; // <-- a pointer to a memory location that contains an array of characters, terminated with a zero value.char *pstringVariable; // <-- the exact same as above.PCSTRINGZ pstringVariable; // <-- the exact same as above.pstringVariable = (PCSTRINGZ)malloc(21); <-- allocates the memorysprintf(pstringVariable,"%1.0f",22.0); <-- formats the value of 22.0 into the pstringVariable character array.

Share this post


Link to post
Share on other sites

Hi Ed,Thank you for the example. Now one extra basic question :-)I have int a = 20 (or INT32, UINT32, etc)int b = 30 (or FLOAT64, etc)char Text[7]="Pointer"Now I want to make an array that holds "2030Pointer" and be of type PCSTRINGZ, for example PCSTRINGZ psvalue, where psvalue holds "2030Pointer".....???Tom

Share this post


Link to post
Share on other sites

>Hi Ed,>>Thank you for the example. >Now one extra basic question :-)>>I have >>int a = 20 (or INT32, UINT32, etc)>int b = 30 (or FLOAT64, etc)>>char Text[7]="Pointer">>Now I want to make an array that holds "2030Pointer" and be of>type PCSTRINGZ, for example PCSTRINGZ psvalue, where psvalue>holds "2030Pointer".....???>>Tom>>>>Ok... then do this:PCSTRINGZ mystringPtr;mystringPtr = (PCSTRINGZ)malloc(128); // <-- 128 bytes, plenty of room for what you want.sprintf(mystringPtr,"%d%d%s",a,b,Text);Just remember... if you allocate memory... deallocate it when done:delete(mystringPtr);

Share this post


Link to post
Share on other sites

Thank you Ed, Actually I don't want to output mystringPtr to the console, but rather hold a+b+Text "inside" mystringPtr and be able to pass mystringPtr as an argument of a function like MyFunct(PCSTRING mystringPtr, etc, etc).Anyway I have a good start point, will do some testings and see if I can nail it down.Thanks againTom

Share this post


Link to post
Share on other sites

sprintf is how you take 2 integers and a string and combine them into a string. Has nothing to do with console output. sprintf is a string formatting function.

Share this post


Link to post
Share on other sites

Yes, you're right Ed, I confused printf with sprintf...Now I have it working like a charm, not like your example but in the inverse way:sprintf(mystringPtr,"%d%d%s",a,b,Text) doesn't work because sprintf cannot convert the PCSTRINGZ format to the char* format supported in the first parameter. Working with a char type and assigning the result to a PCSTRINGZ does the trick:int a = 5;int b = 23;char pMyBuffer[10]; sprintf_s(pMyBuffer,"%d%d%s",a,b,"1234567");PCSTRINGZ mystringPtr = pMyBuffer;now I have "5231234567" in mystringPtr, and no need to alloc/dealloc memory.Thank you very much for your help!Tom

Share this post


Link to post
Share on other sites

sprintf doesn't have to convert... you do.sprintf_s((char*)mystringPtr,"%d%d%s",a,b,Text);Typecasting is a rather 'basic' concept in C/C++.

Share this post


Link to post
Share on other sites

Yeah, maybe, but it happens that I'm still in "pre-basic" stages. Anyway I have plans to improve quickly...:-)Tom

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this