CString to char array convertion (vice versa)

T

Tim Wong

All:

I am trying to convert a CString value to an unsigned char array.
I found some code online that will allow me to compile, but when I try
to print out...i get a whole mess.

/*Begin Code*/
CString day("01");
unsigned char testDay[2];

//Code snippet found online
strncpy((char *) testDay, (LPCTSTR) day, sizeof(testDay));

printf(day);

/*End Code*/

Up there i am trying to "cast" the Cstring into an unsigned char[2].
Any suggestions? What if it were the other way around...how would you
go about doing that?
 
H

Howard

Tim Wong said:
All:

I am trying to convert a CString value to an unsigned char array.
I found some code online that will allow me to compile, but when I try
to print out...i get a whole mess.

/*Begin Code*/
CString day("01");
unsigned char testDay[2];

//Code snippet found online
strncpy((char *) testDay, (LPCTSTR) day, sizeof(testDay));

printf(day);

/*End Code*/

Up there i am trying to "cast" the Cstring into an unsigned char[2].
Any suggestions? What if it were the other way around...how would you
go about doing that?

You'd be better off asking in a VC++ or Windows newsgroup, I think.

But I can see at least one problem right away. You're casting the CString
(an object) as a pointer (LPCTSTR, which, if I recall, is a "long pointer to
constant T-string"). That is simply not a valid cast. There should be a
member function or variable inside CString that allows you to access its
internal string data. Read your doc's or your on-line manual, or ask in a
newsgroup that knows about CString.

-Howard
 
M

Mike Wahler

Tim Wong said:
All:

I am trying to convert a CString value to an unsigned char array.
I found some code online that will allow me to compile, but when I try
to print out...i get a whole mess.

/*Begin Code*/
CString day("01");
unsigned char testDay[2];

//Code snippet found online
strncpy((char *) testDay, (LPCTSTR) day, sizeof(testDay));

printf(day);

/*End Code*/

Up there i am trying to "cast" the Cstring into an unsigned char[2].
Any suggestions? What if it were the other way around...how would you
go about doing that?

Ask about all that MFC and Windows stuff in an appropriate
newsgroup:

#include <iostream>
#include <string>

int main()
{
std::string day("01");
std::cout << day << '\n';
return 0;
}

Look, Ma, no arrays, no C library functions, no MFC
(nor is any of that needed).

-Mike
 
J

Jeff Flinn

Why do you need an unsigned char array?
I found some code online that will allow me to compile, but when I
try to print out...i get a whole mess.

/*Begin Code*/
CString day("01");
unsigned char testDay[2];


You didn't leave room for the trailing '/0'.


I don't use strncpy, is testDay the destination? Also if this is a unicode
build you

Did you mean testDay?
/*End Code*/

Up there i am trying to "cast" the Cstring into an unsigned char[2].
Any suggestions? What if it were the other way around...how would
you go about doing that?

You'd be better off asking in a VC++ or Windows newsgroup, I think.

But I can see at least one problem right away. You're casting the
CString (an object) as a pointer (LPCTSTR, which, if I recall, is a
"long pointer to constant T-string"). That is simply not a valid
cast. There should be a member function or variable inside CString
that allows you to access its internal string data.

In this case CString::eek:perator (LPCTSTR)()const; is that member function.

Jeff
 
H

Howard

In this case CString::eek:perator (LPCTSTR)()const; is that member function.

Jeff

Cool. I didn't know that. (Which is another good reason VC++-specifc
questions belong in a newsgroup where they KNOW such VC++ things!)

But... since that's the case, why the cast? A conversion operator returns a
value of the type specified, so no cast is needed. You should be able to
just use the CString variable wherever an LPCTSTR is called for (as is
confirmed by a perusal of the online help).

-Howard
 
T

Tim Wong

My apologies for posting this in the incorrect group (still new to this
newsgroup posting).

It turns out the code I initially wrote is correct.

The casting actually takes the string and puts it into "testDay"
without the null terminator. Because of this, it looks funny when it
prints out. I knew that there was no null terminator, but did not
realize it would print out in a wierd way.

Sorry for the hastle. Thanks for the replys
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top