473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

CString to char array convertion (vice versa)

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?

Jul 22 '05 #1
5 14042

"Tim Wong" <ti************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Jul 22 '05 #2

"Tim Wong" <ti************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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
Jul 22 '05 #3
Howard wrote:
"Tim Wong" <ti************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
All:

I am trying to convert a CString value to an unsigned char array.
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'.

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

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

printf(day);
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::operator (LPCTSTR)()const; is that member function.

Jeff
Jul 22 '05 #4

"Jeff Flinn" <NO****@nowhere.com> wrote in message
news:cs**********@bluegill.adi.com...
Howard wrote:
"Tim Wong" <ti************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
All:
//Code snippet found online
strncpy((char *) testDay, (LPCTSTR) day, sizeof(testDay));
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::operator (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
Jul 22 '05 #5
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

Howard wrote:
"Jeff Flinn" <NO****@nowhere.com> wrote in message
news:cs**********@bluegill.adi.com...
Howard wrote:
"Tim Wong" <ti************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
All:
//Code snippet found online
strncpy((char *) testDay, (LPCTSTR) day, sizeof(testDay));
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::operator (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


Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
8
by: Oskar | last post by:
Hi. I`m new in cpp and i have a litlle problem. i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv 234") and i want to put the data (space separated) into an array.It shuld...
3
by: nsyforce | last post by:
What is the correct way to convert a const char* to a CString? I'm somewhat of a newbie and have tried several ways. While they all convert ok, I'm using a profiler that shows a memory leak for...
3
by: John Salerno | last post by:
I think I might create an enumeration with the values Off, Red, Yellow, Blue, and Overloaded and I might use this for bitwise comparison. So, when initializing the arrays, if I wanted to load the...
1
by: Elioth | last post by:
Hi... I need to know how to convert Char Array to Byte Array and vice-versa in VB 2K5 Thanks for all help. Elioth
4
by: Cactus | last post by:
How to convert unsigned char* to CString: I wrote some function: u_char_ =55; u_char_ =66; u_char_ =77; .........=ii...... Convert_to_CS(u_char_);
1
by: nd3r | last post by:
Hy! The problem, is that I tried everything I could to convert. The following examples are already useless: //Example 1: CString text; char *ch_text = new char; strcpy(ch_text,text);
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
4
by: HackerisNewKnight | last post by:
Hello, 1) First problem. In my project i need to convert from char* to BYTE*( BYTE is unsigned char), is there any way to make the convertion? BYTE* bite; char* ch="help me, please"; bite =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.