Different String formats

Ok, so I've read up on ASCII and unicode and I'm pretty sure I understand. What I don't understand is how to get from one kind of string to another. For instance, Let's say I'm writing a program with .net so I'm using System::String but I want to use wmp3lib to play an mp3, which takes a char * for the filename, how do I get from one to another? Same problem when using WIN32? I know I can use filename.ToCharArray but that returns a cli::array<char>* and not a standard char*. Some point me in the right direction please?
Hey,

First of all: I hate C++/CLI it is soooooo ugly.

In C++/CLI you can use all(?) the standard-classes of ISOC++ ( <-- That's the real C++ ). That means:

If you are using a library, that want's a const char*, your code would look like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string> // for std::string
#include <Mp3Library>


std::string MyLovelySong ("Titanic-Celine Dion.mp3") ;

Mp3Library::PlaySong(MyLovelySong .c_str()) ; // Mp3Library::PlaySong(..) want's a const char* so we call the method of std::string, which is 'c_str()' and he is giving us a const char*.

// and if the function would like have a char*, your code would look like so:

char* MyLovelySong = "House from Ibiza 107-Sebastien.mp3" ;

Mp3Library::PlaySong(MyLovelySong) ;



bye and have fun (:
Fine, if I already know the file I want to play when before I compile the program. What I need is something like this:
 
PlaySong(fileChooser1->filename->AppropriateConversionFunction());
Last edited on
Hey,

I don't know the signature of those objects. What is 'fileChooser1' , 'filename' ?
Are they Instance of classes?

If 'filename' is a System::String^, you can do this:

From Visual C++ 2008 there is a library, which is called 'marshal_as'. This library is for such problems programmed.
http://msdn.microsoft.com/en-us/library/bb384859.aspx
The use is easy:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <string> 
#include <tchar.h> 
#include <msclr/marshal.h> 
#include <msclr\marshal_cppstd.h> 
using namespace System; 
using namespace msclr::interop; 

int main(array<String ^> ^args) 
{ 
  // TO String^ 
  String ^s1 = marshal_as<System::String^>("Hello world"); 
  Console::WriteLine(s1); 

  String ^s2 = marshal_as<System::String^>(L"Hello world"); 
  Console::WriteLine(s2); 

  String ^s3 = marshal_as<System::String^>(_T("Hello world")); 
  Console::WriteLine(s3); 

  String ^s = "Hello world"; 

  // FROM String^ 
  std::string us1 = marshal_as<std::string>(s); 
  printf(us1.c_str()); 

  std::wstring us2 = marshal_as<std::wstring>(s); 
  wprintf(us2.c_str()); 

  msclr::interop::marshal_context c;  // Context kann wiederverwendet werden! 

  const char *us3 = c.marshal_as<const char*>(s); 
  printf(us3); 

  const wchar_t *us4 = c.marshal_as<const wchar_t*>(s); 
  wprintf(us4); 

  LPCTSTR us5 = c.marshal_as<LPCTSTR>(s); 
  _tprintf(us5); 
}



A nice thing, right?


bye and have fun (:
I have Visual Studio 2008, but I don't have these files. Are they avaliable in a library I can download? I downloaded marshal.h, but I still don't have the files I need to go with that e.g. glib-object.h
Topic archived. No new replies allowed.