Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do i convert between System::String and char* ?

Posted on 2006-10-30
1
Medium Priority
?
251 Views
Last Modified: 2013-11-20
Hello. (Simple newbie questions:)

In VC++ 2005 Express Edition:

1. How do i convert from "String^" to "char*" ?
2. How do i convert from  "char*" to "String^" ?

Thank you.
0
Comment
Question by:Catalin80
1 Comment
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 17832409
#include "stdafx.h"

using namespace System;
using namespace System::Runtime::InteropServices;

#include <stdio.h>

int main(array<System::String ^> ^args)
{
    // char*" to "String^

    char* p = "abcde";
    String^ s = gcnew String(p);

    Console::WriteLine(s);

    // String^" to "char*
    IntPtr ptr = Marshal::StringToHGlobalAnsi(s);
    char* p1 = (char*)ptr.ToPointer();

    printf(p1);
    Console::WriteLine();

    Marshal::FreeHGlobal(ptr);

    return 0;
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question