Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-10-30
1
Medium Priority
?
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

721 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