BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

char to string conversion

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Students)
View previous topic :: View next topic  
Author Message
Roger
Guest





PostPosted: Mon Apr 09, 2007 9:40 pm    Post subject: char to string conversion Reply with quote



I am having trouble with a char to string conversion, e.g.

char *pStr;
pStr = (char *)lpDevCaps + lpDevCaps->dwLineNameOffset; // points to
"Some Text";

string sText = pStr; // sText = { }

How can I set sText to equal "Some Text" ???

Roger
Back to top
Thomas Maeder [TeamB]
Guest





PostPosted: Mon Apr 09, 2007 10:19 pm    Post subject: Re: char to string conversion Reply with quote



Roger <aretae (AT) magma (DOT) ca> writes:

Quote:
I am having trouble with a char to string conversion, e.g.

char *pStr;
pStr = (char *)lpDevCaps + lpDevCaps->dwLineNameOffset; // points to
"Some Text";

string sText = pStr; // sText = { }

And what exactly is the trouble?
Back to top
Roger
Guest





PostPosted: Mon Apr 09, 2007 10:44 pm    Post subject: Re: char to string conversion Reply with quote



Quote:
And what exactly is the trouble?

I want sText = "Some Text";


Not sText = { };

Roger
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Apr 09, 2007 11:04 pm    Post subject: Re: char to string conversion Reply with quote

"Roger" <aretae (AT) magma (DOT) ca> wrote in message
news:461a7b5c$1 (AT) newsgroups (DOT) borland.com...

Quote:
I want sText = "Some Text";

Not sText = { };

You did not show what lpDevCaps is to begin with. Obviously, you are
not actually pointing to the right memory offset.


Gambit
Back to top
Roger
Guest





PostPosted: Tue Apr 10, 2007 12:30 am    Post subject: Re: char to string conversion Reply with quote

Quote:
You did not show what lpDevCaps is to begin with. Obviously, you are
not actually pointing to the right memory offset.


Gambit


My apologies, I was obviously not clear. I did use debug to inspect
pStr which is actually equal to "AOpen FM56-EXV".

and debug showed sText = { };

So I know pStr is pointing to "Some Text", what I don't know is why I
can't set

string sText = what pStr is point to, i.e. in my specific instance
"AOpen FM56-EXV"

instead sText = { }

EXCEPT now I just tried it again, and it WORKS!!! I have no idea why it
didn't work for a couple of hours, I don't know what I did between the
time it didn't and now to make it work. I can't replicate the original
problem.

What I am doing now that seems to work is:

string sText = (char *)lpDevCaps + lpDevCaps->dwLineNameOffset; //
for debug

Roger
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Apr 10, 2007 12:40 am    Post subject: Re: char to string conversion Reply with quote

"Roger" <aretae (AT) magma (DOT) ca> wrote in message
news:461a9443$1 (AT) newsgroups (DOT) borland.com...

Quote:
My apologies, I was obviously not clear. I did use debug to
inspect pStr which is actually equal to "AOpen FM56-EXV".

and debug showed sText = { };

That would not be possible given the code you have shown. sText would
contain the actual string value. Now, it could be that the debugger
itself simply can't show the contents of sText correctly. That is a
whole separate issue.

Quote:
So I know pStr is pointing to "Some Text", what I don't know is
why I can't set

string sText = what pStr is point to, i.e. in my specific instance
"AOpen FM56-EXV"

instead sText = { }

The code you showed suggests that you are using a std::string from the
STL. The STL is not the easiest thing to debug because it uses a lot
of internal structures. Try using AnsiString as a test and see what
happens. The debugger can handle AnsiString very easily. If it
contains the desired result, the the code itself is fine.


Gambit
Back to top
Roger
Guest





PostPosted: Tue Apr 10, 2007 12:55 am    Post subject: Re: char to string conversion Reply with quote

Quote:
The code you showed suggests that you are using a std::string from the
STL. The STL is not the easiest thing to debug because it uses a lot
of internal structures. Try using AnsiString as a test and see what
happens. The debugger can handle AnsiString very easily. If it
contains the desired result, the the code itself is fine.


Thanks Remy, I will try and remember this.
Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Tue Apr 10, 2007 12:56 am    Post subject: Re: char to string conversion Reply with quote

Roger <aretae (AT) magma (DOT) ca> writes:

Quote:
What I am doing now that seems to work is:

string sText = (char *)lpDevCaps + lpDevCaps->dwLineNameOffset;

Be wary of code that "seems" to work! I can't help but wonder why you
need the cast? What is the type of lpDevCaps?

--
Chris (TeamB);
Back to top
Roger
Guest





PostPosted: Tue Apr 10, 2007 1:02 am    Post subject: Re: char to string conversion Reply with quote

Chris Uzdavinis (TeamB) wrote:
Quote:
Roger <aretae (AT) magma (DOT) ca> writes:


What I am doing now that seems to work is:

string sText = (char *)lpDevCaps + lpDevCaps->dwLineNameOffset;


Be wary of code that "seems" to work! I can't help but wonder why you
need the cast? What is the type of lpDevCaps?


If I leave the cast out I get the following error:


[C++ Error] unitTAPI.cpp(188): E2034 Cannot convert 'linedevcaps_tag *'
to 'string'

lpDevCaps is defined as:

LINEDEVCAPS *lpDevCaps = NULL;

Roger
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Tue Apr 10, 2007 1:45 am    Post subject: Re: char to string conversion Reply with quote

"Roger" <aretae (AT) magma (DOT) ca> wrote in message
news:461a9bc5$1 (AT) newsgroups (DOT) borland.com...

Quote:
If I leave the cast out I get the following error:

[C++ Error] unitTAPI.cpp(188): E2034 Cannot convert 'linedevcaps_tag
*'
to 'string'

The error is correct in that particular situation, as lpDevCaps is a
pointer to a structure.

Quote:
lpDevCaps is defined as:

LINEDEVCAPS *lpDevCaps = NULL;

That would have been really useful information to know earlier. In
which case, your code should be more like the following instead:

char* pName = (char*) (((LPBYTE) lpDevCaps) +
lpDevCaps->dwLineNameOffset);
string sText(pName, lpDevCaps->dwLineNameSize);

You can't rely on the name being null-terminated. That is why
Microsoft includes the dwLineNameSize member.


Gambit
Back to top
Roger
Guest





PostPosted: Tue Apr 10, 2007 3:25 am    Post subject: Re: char to string conversion Reply with quote

Quote:
That would have been really useful information to know earlier. In
which case, your code should be more like the following instead:

char* pName = (char*) (((LPBYTE) lpDevCaps) +
lpDevCaps->dwLineNameOffset);
string sText(pName, lpDevCaps->dwLineNameSize);

You can't rely on the name being null-terminated. That is why
Microsoft includes the dwLineNameSize member.


Gambit


Thanks Remy, I changed my code accordingly.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Students) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.