473,386 Members | 1,820 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,386 software developers and data experts.

VC++: .NET 'String' to 'char *' and non-.NET Classes

Hi all;

A) What's the simplest way to convert a 'String' to 'char *' (and the other
way araound) ?
B) I have some C++ classes that I cannot easily convert to .NET (because
they still are to be used in non-.NET apps). Are there any critical issues
why i cannot use these 'as is' in my .NET app (beside of the 'new' and
'delete' of them to 'manually' maintain memory) ?
---------------
Frank R Eid, Norway

Nov 17 '05 #1
8 3808
"Frank R Eid" <Fr*******@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
A) What's the simplest way
Simplest is subjective. What follow is my view:
to convert a 'String' to 'char *'
#include <vcclr.h>

wchar_t __pin *pStr = PtrToStringChars(s);

Note that .Net uses 16 bit (UNICODE) characters.
(and the other way araound) ?


char __nogc *p;
System::String *s;

s = new String(p, 0, lstrlen(p));

Regards,
Will
Nov 17 '05 #2
Hi =?Utf-8?B?RnJhbmsgUiBFaWQ=?=,

A) What's the simplest way to convert a 'String' to 'char *' (and the
other way araound) ?


See: Convert from System::String* to TCHAR*/CString
http://blog.kalmbachnet.de/?postid=18

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #3
> #include <vcclr.h>

wchar_t __pin *pStr = PtrToStringChars(s);


Thanks. but,

I was a bit hasty with the first message. What I ment was;

How to convert a .NET 'String' (16 bit) to a 'char *' (8 bit) with some sort
of unicode-to-ansi convertion.
The reason for doing this is the implementation of old C++ classes that I
cannot easily convert to .NET ...

Regards Frank.

Nov 17 '05 #4
> See: Convert from System::String* to TCHAR*/CString
http://blog.kalmbachnet.de/?postid=18


Thanks. Got it.

Frank.
Nov 17 '05 #5
"Frank R Eid" <Fr*******@discussions.microsoft.com> wrote in message
news:D9**********************************@microsof t.com...
Thanks.
You are welcome.
What I ment was; How to convert a .NET 'String'
(16 bit) to a 'char *' (8 bit) with some sort
of unicode-to-ansi convertion.


You have at least three more options:

1) .Net's Marshall::StringToHGlobalAnsi() which allocates
memory for the conversion and which you will have to
remember to free

2) the plain old crt function wcstombs()

3) Win32's WideCharToMultiByte().

Regards,
Will
Nov 17 '05 #6
> You have at least three more options:

Thanks again;

Going for the 'Marshall::StringToHGlobalAnsi()' sulution.

Frank

Nov 17 '05 #7
Hi =?Utf-8?B?RnJhbmsgUiBFaWQ=?=,
How to convert a .NET 'String' (16 bit) to a 'char *' (8 bit) with
some sort of unicode-to-ansi convertion.
The reason for doing this is the implementation of old C++ classes
that I cannot easily convert to .NET ...


You should also be aware, that "StringToHGlobalAnsi" will always use CP_ACP
as conversion...

See: What is an ANSI string?
http://blog.kalmbachnet.de/?postid=19

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #8
Frank R Eid wrote:
Hi all;

A) What's the simplest way to convert a 'String' to 'char *' (and the other
way araound) ?

The natural candidate is wchar_t * (or better wstring). In both cases
String provides a member function ToCharArray that returns a managed,
*non-null-terminated*, wchar_t array.
You can do this for example:
#using <mscorlib.dll>

#include <string>

int main()
{
using namespace std;
using namespace System;

String *S= __gc new String("Some string");

wchar_t __pin *temp= &(S->ToCharArray())[0];

wchar_t *p=temp;

wstring s(p, p+3);

temp=0;
}
In your case where you want to copy it in a char * you can do this:
#using <mscorlib.dll>

int main()
{
using namespace System;

String *S= __gc new String("Some string");

// Unmanaged char array in the heap
char *pchar= new char[S->Length+1];

wchar_t __pin *temp= &(S->ToCharArray())[0];

for(long i=0; i<S->Length; ++i)
pchar[i]= temp[i];

pchar[S->Length]='\0';

// Unpin
temp=0;
}
The opposite is easy:

#using <mscorlib.dll>

int main()
{
using namespace System;

char sometext[]= "Some Text";

String *S= __gc new String(sometext);
}
B) I have some C++ classes that I cannot easily convert to .NET (because
they still are to be used in non-.NET apps). Are there any critical issues
why i cannot use these 'as is' in my .NET app (beside of the 'new' and
'delete' of them to 'manually' maintain memory) ?

No problems, apart from when using them with managed types (like a
vector<Button *>). This will become easier in VC++ 2005.
Nov 17 '05 #9

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

Similar topics

2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
7
by: Mariano López | last post by:
Hi, I found a program on the net and I'm having some problems with it. Basically what this program does is communicate with Winamp, but it can use the functions that are supposed to work only for...
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
15
by: Ramaraj M Bijur | last post by:
Hi All, Could anyone help me to resolve following problem in C the IDE used is Microsoft VC++, Please let me know the which option in VC++ will do the needful The problem statement:...
11
by: Tatu Portin | last post by:
Have this kind of struct: typedef struct { char **user_comments; /* ... */ } vorbis_comment; /* prototype */ char * read_vorbis_string ( FILE *sc);
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
0
by: Leonid | last post by:
Hello, Please help me to resolve next problem: I have Web service installed on the network and I can communicate with it via WSDL file from several applications including VC++6 application...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
3
by: =?Utf-8?B?RGlwZXNoX1NoYXJtYQ==?= | last post by:
Hi all, I am porting my code in VC++ to VC.net i.e managed. I have seen somewhere that i need to convert my char* to String*, but String * doesnt perform pointer arithmetic like String* p; *p++='...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.