int to std::string to System::String 
Author Message
 int to std::string to System::String

For the last 20 years I have been using code like:

char buf[50];
sprintf( buf, "%d", val );
DebugMessage( buf  );

but now I want to stop using the C libraries and use the C++ standard
library.
What is the best way to do this with the C++ standard library.

Here is what I came up with:

ostringstream msgStream;
msgStream << val;
DebugMessage( msgStream.str() );

I could have just used managed C++ and System::String, but down in the
depths of my code I want it to be standard C++.

In my managed C++ layer, I want to convert the std::string to
System::String.

Here is what I have.

void CMessageDispatcher::DebugMessage( string msg )
{
_pMessageHandler->DebugMessage( msg.c_str() );

Quote:
}

The message handler is defined in as:

// MC++
gcroot<IMessageHandler*> _pMessageHandler;

// C#
public interface IMessageHandler
{
void DebugMessage( string msg );

Quote:
}

thanks

Bill



Mon, 28 Nov 2005 01:51:26 GMT  
 int to std::string to System::String
Hi Bill,

I am not quite sure what the question is you are asking. Could you make it
clear, thanks for your cooperation.

Have a nice day!

Regards,
HuangTM
This posting is provided "AS IS" with no warranties, and confers no rights.



Mon, 28 Nov 2005 23:31:41 GMT  
 int to std::string to System::String
Another way to put question 1 is:  How do you convert various types to
std::string?  Are there any conversion functions which are not part of the C
run time?  In this section of code I want everything to be standard C++, no
C run time and no .NET code.

Question 2:  How do you convert a std::string into System::String.  Here I
have a thin managed C++ layer to interface between my standard C++ and my C#
code.

Bill



Quote:
> Hi Bill,

> I am not quite sure what the question is you are asking. Could you make it
> clear, thanks for your cooperation.



Tue, 29 Nov 2005 00:53:23 GMT  
 int to std::string to System::String

Quote:

> but now I want to stop using the C libraries and use the C++ standard
> library.
> What is the best way to do this with the C++ standard library.

> Here is what I came up with:

> ostringstream msgStream;
> msgStream << val;
> DebugMessage( msgStream.str() );

This is generally the C++ way to do things.

Quote:
> void CMessageDispatcher::DebugMessage( string msg )
> {
> _pMessageHandler->DebugMessage( msg.c_str() );
> }

This also looks fine; converting via std::string->C-string->System::String is
probably the easiest way.  You can do .c_str() at the call site or, as you
have done, write a little wrapper to do it for you.

Ken



Tue, 29 Nov 2005 01:49:49 GMT  
 int to std::string to System::String
Hi Bill,

Thanks for your reply. Your code looks great, and that's the way it should
be.

Quote:
>> How do you convert various types to std::string?

Use stream classes are the C++ way.

Quote:
>> How do you convert a std::string into System::String.

As you know, the std::string is not necessarily null terminated, so we have
to convert it to C-style (by using c_str method) and assign it to
System.String in .NET.

Have a nice day!

Regards,
HuangTM
This posting is provided "AS IS" with no warranties, and confers no rights.



Tue, 29 Nov 2005 20:04:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. static std::string exposed through System::String *

2. System::String to std::string

3. System.String to std::string conversion

4. convert non std::string to std::string

5. how to convert int to std:string?

6. convert int to std::string is nasty

7. String to std:string

8. null-terminated string to std::string

9. using std::string and string::npos...

10. Assignind a NULL C string to an std::string

11. convert between std::string and std::wstring

12. Warnings for std::vector<std::string>

 

 
Powered by phpBB® Forum Software