How to convert char * to a System::string ^ [duplicate] - string

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the best way to convert between char* and System::String in C++/CLI
Hello,
I have a function in a c++ project using \clr something like:
int WINAPI dmtTest(char *pcertificate)
{
String^ sCertificate;
sCertificate = pcertificate; // how can I assign pcertificate to sCertificate?
....
}

You can do:
String^ sCertificate;
sCertificate = gcnew String(pcertificate);

Related

Changing the text of a C++ CLI label

I am trying to change the text of a label in a C++ CLI program. I need to take a value the the user entered in a textbox, insert that into a short string, then change a label to that string. I have no problem constructing the string, but I am having trouble setting the label to the new string. Here is my code...
std::string v1str = "Phase A: ";
v1str.append(vt2); //vt2 is type str::string
v1str.append(" Vac");
label->Text = v1str;
This is the error message that I'm getting...
Why am I not allowed to pass v1str as the label text setter? How can I pass the string I've constructed to the label text setter?
Label::Text has a type of System::String^, which is a managed .Net string object. You cannot assign a std:string to a System::String^ directly becuase they are different types.
You can convert a std::string to a System::String. However you most likely just want to use the System::String type directly:
System::String^ v1str = "Phase A: ";
v1st += vt2; // or maybe gcnew System::String(vt2.c_str());
v1str += " Vac";
label->Text = v1str;
C++/CLI is not C++, you can't use std::string in there. But you can use C++ within C++/CLI, and convert std::string to and from System::String
//In C++/CLI form:
#include <vcclr.h>
System::String^ clr_sting = "clr_sting";
//convert strings from CLI to C++
pin_ptr<const wchar_t> cpp_string = PtrToStringChars(clr_sting);
//convert strings from C++ to CLI
System::String^ str = gcnew System::String(cpp_string);
//or
std::string std_string = "std_string";
System::String^ str2 = gcnew System::String(std_string.c_str());

Converting int to string without printing [duplicate]

This question already has answers here:
How can I convert an int to a string in C?
(10 answers)
Closed 8 years ago.
I want to convert an int to a string without printing anything on my screen. For now I used sprintf, but this also printed the int to my screen.
Also itoa is not supported by my compiler so I can't use that either.
I assume You use ANSI C.
You cannot use itoa because it's not a standard function.
sprintf or snprintf is dedicated to that.
Since You do not want to use sprintf, make your own itoa instead:
#include <stdio.h>
char* itoa(int i, char b[]){
char const digit[] = "0123456789";
char* p = b;
if(i<0){
*p++ = '-';
i *= -1;
}
int shifter = i;
do{ //Move to where representation ends
++p;
shifter = shifter/10;
}while(shifter);
*p = '\0';
do{ //Move back, inserting digits as u go
*--p = digit[i%10];
i = i/10;
}while(i);
return b;
}
original answer: here

How to convert String^ to String in C++ CLI?

Whenever I try to directly assign a String variable to another String variable I get error found no suitable conversation.
So is there a way to convert String^ pointer to a non-pointer struct String ?
I want :
System::String a = System::String('X',256);
I don't want :
System::String^ a = %System::String('X',256);
No, there is not, because as Hans pointed out in a comment, System::String is immutable. You cannot assign it. You can only associate a handle with an entirely new System::String object.
BTW
System::String^ a = %System::String('X',256);
is incorrect, it should be
System::String^ a = gcnew System::String('X',256);
Use System::String a('X', 256);.

SHGetSpecialFolderPath to a System::String^

I'm trying to parse a SHGetSpecialFolderPath into a string, a System::String^ to be precise.
I'm now using this code:
TCHAR appDataPath;
SHGetSpecialFolderPath(0, appDataPath, CSIDL_APPDATA, false);
I've tried things like this, but it doesn't work either:
LPWSTR appDataPath;
SHGetSpecialFolderPath(0, appDataPath, CSIDL_APPDATA, false);
I just want to get a System::String^ like this:
System::String ^ loc_inst = appDataPath + "\\inst\\info.xml";
I don't think C++/CLI can automatically concatenate char arrays and assign them to a string handle. I think you need to instantiate a new System::String object like this:
System::String^ loc_inst = gcnew System::String(appDataPath);
loc_inst.Append("\\inst\\info.xml");
Or you could use a StringBuilder, but if you want to make a new String object I think you've got to use gcnew and a constructor.
Keep in mind that appDataPath is not a String but a char array that you have previously allocated. However, System::String allows you to pass in char arrays in one of its constructors.

C++/CLI String Conversions

I found this really nice piece of code that converts a string to a System:String^ as in:
System::String^ rtn = gcnew String(move.c_str()); // 'move' here is the string
I'm passing rtn back to a C# program. Anyways, inside the function where this code exists, I'm passing in a System::String^. I also found some code to convert a System:String^ to a string using the following code:
pin_ptr<const wchar_t> wch = PtrToStringChars(cmd); // 'cmd' here is the System:String
size_t convertedChars = 0;
size_t sizeInBytes = ((cmd->Length + 1) * 2);
errno_t err = 0;
char *ch = (char *)malloc(sizeInBytes);
err = wcstombs_s(&convertedChars,ch, sizeInBytes,wch, sizeInBytes);
Now I can use 'ch' as a string.
This, however, seems to be alot more work than converting the other way using the gcnew. So, at last my question is, is there something out there that will convert a System::String^ to string using a similar fashion as with the gcnew way?
Use VC++'s marshaling library: Overview of Marshaling in C++
#include <msclr/marshal_cppstd.h>
// given System::String^ mstr
std::string nstr = msclr::interop::marshal_as<std::string>(mstr);
this could be useful:
wchar_t *str = "Hi StackOverflow"; //native
String^ mstr= Marshal::PtrToStringAnsi((IntPtr)str); // native to safe managed
wchar_t* A=( wchar_t* )Marshal::StringToHGlobalAnsi(mstr).ToPointer(); // return back to native
don't forget using namespace System::Runtime::InteropServices;

Resources