473,625 Members | 2,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14065

"Tim Wong" <ti************ @gmail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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.goo glegroups.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.goo glegroups.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::operat or (LPCTSTR)()cons t; 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.goo glegroups.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::operat or (LPCTSTR)()cons t; 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.goo glegroups.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::operat or (LPCTSTR)()cons t; 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
22722
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 the std::string a pointer and a length and then give the std::string control over the pointer and its content? I'm basically trying to avoid copying large text between an std::string and a char pointer, and vice versa. Is there anyhing in the...
8
2304
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 be somethign like this. array1="aaa"; array1="bbb"; .... array1="234";
3
9133
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 every option. Here's what I have tried: const char* test; test = getMyChar(); //CString myCString((LPCTSTR)test); //CString myCString(test); CString myCString = new CString(test);
3
1397
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 values "Off" or "Red" into my arrays instead of "0" and "1", would this still be a string array? Or would it be an array of the type of my enum? I would be reading strings from the text file, I guess, but I wasn't sure if this would be...
1
19189
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
11028
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
3344
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
3258
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
2768
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 = (BYTE*)ch; ? is this not right? what shoul i do here?
0
8251
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6115
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.