August 16, 200718 yr 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
August 16, 200718 yr 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. Tim http://fsandm.wordpress.com
August 16, 200718 yr Commercial Member 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. Ed Wilson Mindstar AviationMy Playland - I69
August 16, 200718 yr Author 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
August 17, 200718 yr Commercial Member >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); Ed Wilson Mindstar AviationMy Playland - I69
August 17, 200718 yr Author 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
August 17, 200718 yr Commercial Member 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. Ed Wilson Mindstar AviationMy Playland - I69
August 17, 200718 yr Author 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
August 17, 200718 yr Commercial Member 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++. Ed Wilson Mindstar AviationMy Playland - I69
August 18, 200718 yr Author Yeah, maybe, but it happens that I'm still in "pre-basic" stages. Anyway I have plans to improve quickly...:-)Tom
Create an account or sign in to comment